Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Select where in list of list

I have a list of lists with this kind of data:

data = [[[1], 1, "A", [1,2], "ab"],
        [[2], 2, "B", [2,1], "bc"],
        [[2], 2, "C", [2,1], "bc"]]

Is their in python a simple method to get the sublist of all elements which fullfill some specific condition, e.g. get all elements where third element equals to "A"? More general is their a way to pass select where statements on that list of lists?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use a list comprehension:

[l for l in data if l[2] == 'A']

output:

[[[1], 1, 'A', [1, 2], 'ab']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading