Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find element in list

Tags:

python

list

I have a list like

list_a = [(1, 2), (2, 3), (4, 5)]

and now using this list i wanted to find a element which has last value 3 any short method to achieve this? it should return (2,3)

like image 425
OpenCurious Avatar asked May 16 '26 21:05

OpenCurious


1 Answers

For example:

In [1]: list_a = [(1, 2), (2, 3), (4, 5)]

In [2]: next(x for x in list_a if x[1] == 3)
Out[2]: (2, 3)

Note that it returns a single element, not a list of them (seems to be what you are asking). If there are multiple tuples, the first one is returned.

like image 199
Lev Levitsky Avatar answered May 19 '26 11:05

Lev Levitsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!