How to find the last occurrence of an element in a list with Python

2 Answers

0 votes
lst = ['python', 'c', 'c++', 'python', 'java', 'python', 'c', 'c#']

element = 'python'

lst.reverse()

index = lst.index(element)

print('index = ' , len(lst) - index - 1)



'''
run:

index =  5

'''

 



answered Sep 7, 2022 by avibootz
0 votes
lst = ['python', 'c', 'c++', 'python', 'java', 'python', 'c', 'c#']

element = 'python'

index = len(lst) - 1 - lst[::-1].index(element)

print('index = ' , index)



'''
run:

index =  5

'''



 



answered Sep 7, 2022 by avibootz
...