Filter a list of tuples based on condition

Use a list comprehension:

[y for x,y in A if x>2]

Demo:

>>> A=[(1,'A'),(2,'H'),(3,'K'),(4,'J')]
>>> [y for x,y in A if x>2]
['K', 'J']
>>> 

Leave a Comment