0

I have List of lists like:

[[ac,7,0],[ac,6,1],[ac,5,1],[bc,4,1],[bc,3,0],[ac,2,0]]

I need to get all elements of the first list only if

elem[2] == 1 

otherwise, take the all elements of the next list until elem[2] == 1

The result should be:


[ac,6,1]

I'm new in Python, so I don't understand how to do it

Atum
  • 3
  • 4
  • Please explain the last snippet. What should be your final result? Can you show what the output should be? – quamrana May 17 '23 at 14:00
  • @quamrana Expected result it's elements of the second list. Because i need first list in which element[2]==1 – Atum May 17 '23 at 14:15
  • Does `elem = [x for x in l if x[2] == 1][0]` do the trick? Where `l` is whatever variable name your original list of lists is. – SanguineL May 17 '23 at 14:19
  • 1
    Or: `elem = next((x for x in l if x[2] == 1), None)` which will find the first instance or set elem to `None` if there is no such list found where `x[2] == 1`. – SanguineL May 17 '23 at 14:23

0 Answers0