Zanam
Zanam

Reputation: 4807

searching index of tuple from list of tuples

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]

Upvotes: 1

Views: 44

Answers (1)

TemporalWolf
TemporalWolf

Reputation: 7952

[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?

Upvotes: 3

Related Questions