Python find index of element in list

In this Python tutorial, we will learn how to find the index of elements in a list by using Python. In addition, we will learn about Python Lists and Indexes. Moreover, we will discuss all the different scenarios where we find the index of an element.

Python Lists and Indexes

Before we dive into the different ways to find an index, let’s briefly discuss what Python lists and indexes are.

A Python list is a built-in data structure that can contain items of any data type such as integers, floats, strings, and even other lists or complex objects. The items in a list are ordered and mutable, which means you can change their values.

Each item in a list has a unique position known as its index. Python uses zero-based indexing, meaning the index of the first item is 0, the second item is 1, and so on.

Python list.index() method

Python provides a built-in method called index() that you can use on a list to find the index of a particular element. The syntax of the index() method is:

list.index(element, start, end)

Here:

  • element is the item you are searching for.
  • start and end are optional parameters. They define a sub-list within which the search will be made.

The index() method returns the lowest index where the specified element appears. If the element is not found, it raises a ValueError.

Here’s an example of how to use the index() method:

states = ['California', 'Texas', 'New York', 'Florida', 'Illinois']
print(states.index('New York')) 

This list contains five elements, and each of them is a string representing a U.S. state. In Python, the index of the list starts from 0.

So 'California' is at index 0, 'Texas' is at index 1, 'New York' is at index 2, and so on. To find the index of 'New York' we can use the index() method.

Output:

Python find index of element in list
Python find index of element in list

Python Index Handling Non-Existing Elements

As mentioned, the index() method raises a ValueError if the element is not in the list. If you aren’t sure whether the element exists in the list, you might want to use a try/except block to handle this scenario:

try:
    print(states.index('Georgia'))
except ValueError:
    print("The state is not in the list.")

This will print “The state is not in the list.” because ‘Georgia’ does not exist in the states list. This demonstrates how you can find the index of an item in a list and handle situations when the item does not exist in the list.

Output:

Python find index of element in list example
Python find index of element in list example

Finding All Occurrences of an Element using Python Index

The index() method only returns the first occurrence of the element. If you want to find all occurrences, you can use a list comprehension. Here is an example:

cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'New York', 'Phoenix', 'New York']
indexes = [i for i, city in enumerate(cities) if city == 'New York']
print(indexes)

We start with a list of cities, where 'New York' appears three times. The loop finds all the positions that 'New York' are on the list.

It goes through each city in the list along with its position (i), and if the city is 'New York', it adds the position to the indexes list. Then, the print() function prints the list of all positions where ‘New York’ was found in the list.

Output:

Python find an index of all occurrences in a list
Occurrences of an Element using Python Index

Conclusion

In this tutorial, we have learned, finding the index of an element in a Python list is a common operation that can be done easily with the index() method. If the element might not exist, use a try/except block to handle the ValueError. And if you need to find all occurrences of an element, a list comprehension enumerate() can get the job done.