How to get the index of an item in a Python list

Suppose you want to find the index of an item in a list in Python. You can use the built-in index() function to do so. The index() function takes the item you want to search for as its first argument.

So the signature of the index() function is as follows:

list.index(key [, start[, end]])

The start and end arguments are optional actually. You use those to specify the range of the list you want to search. By default the index() function searches the whole lists.

pizza_toppings = ['tomato sauce', 'cheese', 'tuna']

index = pizza_toppings.index('cheese')

# => 1

See Also

6 Non-Programming Books for Programmers