Python - Get the indices of all occurrences of an element in a list

To retrieve the indices of all occurrences of a specific element in a list, Python provides several efficient methods. In this article, we'll explore four different approaches: using a for loop, list comprehension, the enumerate() function, and the index() method with a while loop.

Using for loop

The most straightforward approach is to iterate through the list using indices and compare each element with the target value.

Syntax

for i in range(len(data)):
    if data[i] == element:
        indices.append(i)

Example

def get_indices(element, data):
    indices = []
    for i in range(len(data)):
        if data[i] == element:
            indices.append(i)
    return indices

my_numbers = [3, 5, 2, 3, 8, 3, 1]
element = 3
result = get_indices(element, my_numbers)
print("Indices of element", element, ":", result)
Indices of element 3 : [0, 3, 5]

Using List Comprehension

List comprehension provides a more concise and Pythonic way to create a list of indices in a single line.

Syntax

[i for i in range(len(data)) if data[i] == element]

Example

def get_indices(element, data):
    return [i for i in range(len(data)) if data[i] == element]

my_numbers = [3, 5, 2, 3, 8, 3, 1]
element = 3
result = get_indices(element, my_numbers)
print("Indices using list comprehension:", result)
Indices using list comprehension: [0, 3, 5]

Using enumerate() Function

The enumerate() function provides both the index and element value, making the code more readable and efficient.

Syntax

[i for i, x in enumerate(data) if x == element]

Example

def get_indices(element, data):
    return [i for i, x in enumerate(data) if x == element]

my_numbers = [3, 5, 2, 3, 8, 3, 1]
element = 3
result = get_indices(element, my_numbers)
print("Indices using enumerate:", result)
Indices using enumerate: [0, 3, 5]

Using index() Method with While Loop

This approach uses the builtin index() method repeatedly to find each occurrence, starting the search after the previously found index.

Example

def get_indices(element, data):
    indices = []
    index = -1
    while True:
        try:
            index = data.index(element, index + 1)
            indices.append(index)
        except ValueError:
            break
    return indices

my_numbers = [3, 5, 2, 3, 8, 3, 1]
element = 3
result = get_indices(element, my_numbers)
print("Indices using index() method:", result)
Indices using index() method: [0, 3, 5]

Comparison

Method Readability Performance Best For
For loop High Good Beginners, complex logic
List comprehension High Best Concise, Pythonic code
enumerate() Very High Best When you need both index and value
index() + while Medium Good Large lists with few matches

Conclusion

The enumerate() method with list comprehension is generally the most Pythonic and efficient approach. Use the for loop method when you need to add complex logic, and consider the index() method for sparse occurrences in large lists.

Updated on: 2026-03-27T08:48:20+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements