4

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?

Gabio
  • 9,126
  • 3
  • 12
  • 32
Eliran Turgeman
  • 1,526
  • 2
  • 16
  • 34

2 Answers2

6

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)))
Gabio
  • 9,126
  • 3
  • 12
  • 32
5

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
Leo Arad
  • 4,452
  • 2
  • 6
  • 17