Home > Net >  Select where in list of list
Select where in list of list

Time:03-20

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?

CodePudding user response:

Use a list comprehension:

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

output:

[[[1], 1, 'A', [1, 2], 'ab']]

CodePudding user response:

list(filter(lambda x: x[2] == 'A', data)

output:

[[[1], 1, 'A', [1, 2], 'ab']]
  • Related