Search code examples
pythonlistselectwhere-clause

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?


Solution

  • Use a list comprehension:

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

    output:

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