Is there a way to search just for the first coordinate in a list of tuples?

Viewed 196

Assuming I have a list of tuples like the following:
a = [('a','b'), ('c','d'), ('e','f')]
If I were to execute this line 'a' in a I would get False.
Is there a way to tell python "search the just for the first argument and accept whatever in the second"?
So that I could search something like ('a', *) in a and get True?

2 Answers

Try using any (will return True if any of the elements is logically True) with map (to compare each first element in your tuples):

any(map(lambda x: x[0] == "a", a)))

You can do it will list comprehension

a = [('a','b'), ('c','d'), ('e','f')]
'a' in [i[0] for i in a]

Or for larger search

'a' in {i[0] for i in a}

since finding an item in a set is way faster.
Both exprations will return

True
Related