1

I have a list of tuples as follows:

tupleList = [('a','b'), ('c','d'), ('a','b'), ('a','b'), ('e','f')]

I want to find index of ('a','b') in tupleList.

I am trying the following:

idx = np.where(tupleList == ('a','b'))

but it gives an empty array.

Desired output would be

idx = [0, 2, 3]
Zanam
  • 4,607
  • 13
  • 67
  • 143
  • Lists and tuples don't support the same operations as NumPy arrays; for example, `tupleList == ('a', 'b')` isn't an elementwise comparison. A list of tuples might not be the best data structure to work with. – user2357112 Mar 16 '17 at 20:12
  • Possible duplicate of [How to find all occurrences of an element in a list?](http://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) – TemporalWolf Mar 16 '17 at 20:14

1 Answers1

3
[i for i, t in enumerate(tupleList) if t == ('a', 'b')]

yields

[0, 2, 3]

see How to find all occurrences of an element in a list?

Community
  • 1
  • 1
TemporalWolf
  • 7,727
  • 1
  • 30
  • 50