Python Get Index of Item in List


In this article, we will discuss how to get the index of an item in a list using Python. We will look at multiple different methods for this purpose, and will also discuss how to handle exceptions.

    Table of Contents

  1. Quick Solution
  2. Using the index() Method
  3. Using List Comprehension
  4. Using For Loop
  5. Handling Exceptions
  6. Conclusion

Quick Solution

If you are in hurry and want a quick solution to get the index of an item in a Python list, then following code snippet will help you:

# Get the index of an item in a list
fruits = ['apple', 'banana', 'cherry', 'apple', 'orange']

# Method 1
index = fruits.index('apple')
print(index)  # Output: 0

# Method 2
for index, fruit in enumerate(fruits):
    if fruit == 'apple':
        print(index)
        break  # Stop the loop after finding the first occurrence
# Output: 0

For more details and more methods, read the article below.


1. Using the index() Method

To find the index of an item in a list, you can use the index() method. It returns the index of the first occurrence of the specified item in the list.

For simple use use you can pass the item whose index you want to find as the only parameter.

list_name.index(item)

For more control, you can also pass the starting and ending index as parameters.

list_name.index(item, start, end)

Here:

Here's an example:

Example 1:

fruits = ['apple', 'banana', 'cherry', 'apple', 'orange']

# using index() method to find the index of 'apple'
index = fruits.index('apple')

print(index) # Output: 0

If you want to get index of first occurrence of the item between a range of indexes, then you can pass the starting and ending index as parameters.

Example 2:

fruits = ['apple', 'banana', 'cherry', 'apple', 'orange']

# using index() method to find
# the index of 'apple' between 2 and 4
index = fruits.index('apple', 2, 4)

print(index) # Output: 3

If the item is not present in the list, then the index() method will raise a ValueError exception.

👉 Jump here to handle exceptions


2. Using List Comprehension

From above method we are limited to get only 1st index of the item. If we want to get all the indexes of the item, then we can use List comprehension.

The idea here is to use the enumerate() function within list comprehension to get the index of each item in the list. Then we can use the if statement to check if the item is equal to the item we are looking for. If it is, then we add the index to the list.

Here is example showing how to get the index of an item in a list using list comprehension:

Example 3:

fruits = ['apple', 'banana', 'cherry', 'apple', 'orange']

# using list comprehension to get index of 'apple'
indices = [i for i, fruit in enumerate(fruits) if fruit == 'apple']

print(indices) # Output: [0, 3]

3. Using for Loop

Another way to find the index of an item in a list is to use for loop.

Similar to list comprehension, we will use same concept but with for loop.

Look at the example below:

Example 4:

fruits = ['apple', 'banana', 'cherry', 'apple', 'orange']

# using enumerate() function with for loop
for index, fruit in enumerate(fruits):
    if fruit == 'apple':
        print(index)

Output:

0
3

Exception handling in index() Method

When you use the index() method to find the index of an item that does not exist in the list, a ValueError is raised.

To handle this exception, you should use a try-except while calling the index() method.

Here is how you can do it:

Example 5:

fruits = ['apple', 'banana', 'cherry', 'apple', 'orange']
try:
    index = fruits.index('pineapple')
    print(index)
except ValueError:
    print("Item not found in the list")
# Output: Item not found in the list

Conclusion

In this article, we discussed 3 different ways to find the index of an item in a list in Python.

For your quick learning first section of the article discusses all disccussed methods with examples.

For safe use of index() method, you should use try-except block.

Happy Coding!😎