Get Index of a List Item in Python

1 min read

The index of an item in a list can be found using a list's index() method. First you create the list and then you use the lists index method to search for the item in the list.

animals = ['dog', 'cat', 'fish', 'bird']
bird_pos = animals.index('bird')

print(bird_pos)
# 3

Note in the above code that the index position of the 'bird' item is 3 despite it being the 4th item in the list. This is because lists in Python start from an index position of 0. So the first item in a Python list will have an index value of 0.

animals = ['dog', 'cat', 'fish', 'bird']
dog_pos = animals.index('dog')

print(dog_pos)
# 0

If you try to search for the index of an item that does not exist in a list Python will raise a value error.

animals = ['dog', 'cat', 'fish', 'bird']
tiger_pos = animals.index('tiger')

# ValueError: 'tiger' is not in list


Author picture
Rhett Trickett

Founder at Able

@rhett RhettTrickett rhetttrickett
Python

Join the discussion

Able is a developer community where people build their coding knowledge and careers.

Join with GitHub Join with Twitter