Python List index()

The index() method searches an element in the list and returns its index.

In simple terms, index() method finds the given element in a list and returns its position.

However, if the same element is present more than once, index() method returns its smallest/first position.

Note: Index in Python starts from 0 not 1.


The syntax of index() method for list is:

list.index(element)

index() Parameters

The index method takes a single argument:

  • element - element that is to be searched.

Return value from index()

The index() method returns the index of the element in the list.

If not found, it raises a ValueError exception indicating the element is not in the list.


Example 1: Find position of element in the list

# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']

# element 'e' is searched
index = vowels.index('e')

# index of 'e' is printed
print('The index of e:', index)

# element 'i' is searched
index = vowels.index('i')

# only the first index of the element is printed
print('The index of i:', index)

When you run the program, the output will be:

The index of e: 1
The index of i: 2

Example 2: Index of element not present in the list

# vowels list
vowels = ['a', 'e', 'i', 'o', 'u']

# element 'p' is searched
index = vowels.index('p')

# index is printed
print('The index of p:', index)

When you run the program, the output will be:

ValueError: 'p' is not in list

Example 3: Find index of tuple and list inside a list

# random list
random = ['a', ('a', 'b'), [3, 4]]

# element ('a', 'b') is searched
index = random.index(('a', 'b'))

# index is printed
print("The index of ('a', 'b'):", index)

# element [3, 4] is searched
index = random.index([3, 4])

# index is printed
print("The index of [3, 4]:", index)

When you run the program, the output will be:

The index of ('a', 'b'): 1
The index of [3, 4]: 2