Python List index() Method

Returns the index of the first item with the specified value.

Syntax

Python
list.index(value)

Parameters

ParameterDescription
value Required. The value to search for within the list. This value can be of any type

Example

Python
groceries = ['apples', 'oranges', 'paper towels']
print(f'Index of apples: {groceries.index("apples")}')
print(f'Index of oranges: {groceries.index("oranges")}')

Output

Index of apples: 0
Index of oranges: 1

Notes

If the value does not exist in the list, Python will throw a ValueError exception.

Example

Python
groceries = ['apples', 'oranges', 'paper towels']
print(f'Index of bananas: {groceries.index("bananas")}')

Output

Traceback (most recent call last):
  File "Lists.py", line 2, in <module>
    print(f'Index of bananas: {groceries.index("bananas")}')
ValueError: 'bananas' is not in list