How to find the indexes of the first and last occurrences of an element in array in Python

1 Answer

0 votes
lst = [3, 2, 4, 7, 3, 0, 9, 8, 7, 7, 12, 18]
element = 7

first = lst.index(element)
print(first)

last = len(lst) - 1 - lst[::-1].index(element)
print(last)
 
 
 
 
'''
run:
 
3
9
 
'''

 



answered Dec 7, 2023 by avibootz
...