0

I have a problem, where an element appears twice in a list, but when I want the index, I only get the index of the first one.

list1=['a','b','c','a','d']
for letter in list1:
    print(list1.index(letter))

But I want it to return 3 for the second 'a'. What should I do? Thanks in advance :)

  • 4
    Do you want all the indexes or the index of last occurrence? – Tushar Gupta Mar 06 '22 at 13:11
  • @Tushar: Based on them looping the whole `list`, I'm guessing they really just want `enumerate` (which reduces the work to `O(n)`, where the current design is `O(n**2)`). – ShadowRanger Mar 06 '22 at 13:25

0 Answers0