How to find an index of the first match item in a list with Python

3 Answers

0 votes
a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

i = a_list.index(2)

print(i)

'''
run:

1

'''

 



answered Aug 30, 2018 by avibootz
0 votes
a_list = ['python', 'java', 'c#', 'c++', 'php']

i = a_list.index('java')

print(i)

'''
run:

1

'''

 



answered Aug 30, 2018 by avibootz
0 votes
a_list = ['python', 'java', 'c#', 'c++', 'php']

for i, s in enumerate(a_list):
    if s == 'java':
        print(i)
        break


'''
run:

1

'''

 



answered Aug 30, 2018 by avibootz
...