Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

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

When working with Python lists, a common task is to find the index of a specific item. The problem arises when you need to determine the position of an element within the list, which can be crucial for various operations like updating, removing, or accessing related data.

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

Here are three different ways to get the index of an item in a Python list:

Method 1: Use the index() method

Consider the below code that looks for the entry “cherry” in a list of fruits:

fruits = ['apple', 'banana', 'cherry', 'date']
index = fruits.index('cherry')
print(f"The index of 'cherry' is: {index}")

The index() method is a built-in function for Python lists that returns the index of the first occurrence of the specified item. In this example, we use index() to find the position of 'cherry' in the fruits list.

The output is:

The index of 'cherry' is: 2

This method is straightforward and easy to use, but it raises a ValueError if the item is not found in the list. To handle this, you can use a try-except block or check if the item exists in the list before calling index().

Method 2: Use a for loop with enumerate()

This approach is a little bit “back to basics” but uses the enumerate() function in Python:

fruits = ['apple', 'banana', 'cherry', 'date']
search_item = 'banana'
for index, item in enumerate(fruits):
    if item == search_item:
        print(f"The index of '{search_item}' is: {index}")
        break
else:
    print(f"'{search_item}' not found in the list")

The enumerate() function allows us to iterate over the list while keeping track of both the index and the item. We use a for loop to check each item against our search item. If a match is found, we print the index and break the loop. The else clause after the for loop is executed if the loop completes without finding the item.

The output will be:

The index of 'banana' is: 1

In overall this approach is more flexible as it allows for custom logic and can be easily modified to find all occurrences of an item or perform additional operations.

Method 3: Use a list comprehension with next() and iter()

fruits = ['apple', 'banana', 'cherry', 'date']
search_item = 'date'
index = next((i for i, item in enumerate(fruits) if item == search_item), -1)
print(f"The index of '{search_item}' is: {index}")

This method combines list comprehension with the next() and iter() functions to create a concise one-liner solution. The list comprehension generates an iterator of indices where the item matches the search_item. The next() function returns the first value from this iterator, or -1 if no match is found.

In the case of the above program, the output will be:

The index of 'date' is: 3

This approach is efficient for large lists as it stops searching once a match is found. However, it may be less readable for beginners and doesn't provide as much flexibility for custom logic compared to the for loop method.

Each of these methods has its own advantages and use cases. The index() method is simple and built-in, the for loop with enumerate() offers more control and flexibility, and the list comprehension approach provides a concise solution for more advanced users. Choose the method that best fits your specific needs and coding style.

Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2025. All rights reserved.