'When I add more than one of the same letter like 'e', they both have the same index [duplicate]

list = []
word = 'hello'
for i in word:
    list.append(i)
for i in list:
    print(list.index(i))

output:

0 1 2 2 4

I dont know how to make the second 'l' to have an index of 3 instead of 2. rindex() does not work on for the code that I am making



Solution 1:[1]

Since you're printing just the list indexes which (by definition) are 0 1 2 3 4, you could use a simple range() loop:

for i in range(len(mylist)):
    print(i)

Solution 2:[2]

The index() method returns the position at the first occurrence of the specified value.

To have the index of all the elements you can do like this

_list = []
word = 'hello'
for i in word:
    _list.append(i)
for i in range(len(_list)):
    print(_list[i], _list.index(_list[i], i))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Gordon
Solution 2 BiRD