How to find the index of a tuple in a list of tuples with Python

2 Answers

0 votes
list_of_tuples = [(3, 'python'), (7, 'c'), (8, 'c++'), (5, 'java'), (9, 'c#')]

index = [idx for idx, tpl in enumerate(list_of_tuples) if tpl[1] == 'c++']

print(index)

print(index[0])
 
 
 
 
'''
run:
 
[2]
2

'''

 



answered Jul 8, 2022 by avibootz
0 votes
list_of_tuples = [(3, 'python'), (7, 'c'), (8, 'c++'), (5, 'java'), (9, 'c#')]

index = [tpl[1] for tpl in list_of_tuples].index('c++')

print(index)


 
 
 
'''
run:
 
2

'''

 



answered Jul 8, 2022 by avibootz

Related questions

1 answer 228 views
2 answers 312 views
...