0

I am trying to find out the position of a certain element in a list. In concept, I input an item in the list, and it should return the position of that item. I have written this:

alphabet = ["a", "b", "c", "d", "e"]

letter = "d" #Want the position in the list of this letter.

for i in range(0, len(alphabet)):
    if alphabet[i] == letter:
        position = i
        break

which works, but I feel like there is a more elegant/efficient way of doing it. Any suggestions would be appreciated!

1 Answers1

0

you can use:

alphabet = ["a", "b", "c", "d", "e"]
print(alphabet.index('d'))

It will return the index of the first 'd' into the list

magimix
  • 172
  • 5