Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 18 Jul, 2022

Given a list, the task is to write a Python Program to get the indices of all occurrences of an element in a list.

Method 1: Using For loop

This is a simple method to get the indices of all occurrences of an element in a list. Here we use a for-loop to iterate through each element in the original list.

Syntax : for iterator_name in range(length)

where

  • iterator_name is the name of the iterator
  • length is the size of the list

Example : 

Python3




# initialize a list
my_list = [1, 2, 3, 1, 5, 4]
 
# find length of the list
list_size = len(my_list) 
 
# declare for loop
for itr in range(list_size): 
   
      # check the condition
    if(my_list[itr] == 1): 
       
          # print the indices
        print(itr) 


Output:

0
3

Method 2: Using enumerate() function

Instead of using for-loop we can use enumerate function. This function adds a counter to an iterable and returns the enumerate object.

Syntax: [expression for element_name in enumerate(list_name) if condition]

where

  • Element_name is the name of the element
  • list_name is the name of the list
  • Condition is the condition that needs to be true

For this, we will create list and then we will create enumerate function to get indices of all occurrences of an element in a list

Example :

Python3




# initialize a list
my_list = [1, 2, 3, 1, 5, 4
indices = [ind for ind, ele in enumerate(my_list) if ele == 1]
 
# print the indices
print(indices) 


Output:

[0, 3]

Method 3: Using itertools module

Itertools is memory-efficient tools that are useful by themselves or in combination, so for this we will use count() methods from this module which will return an iterator of evenly spaced values from the start value.

Syntax: [expression for element_name in zip(count(), list_name) if condition]

  • element_name is the name of the element
  • list_name is the name of the list
  • Condition is the condition that needs to be true

For this, we will create a list and then using comprehension with zip() we will get indices of all occurrences of an element in a list. 

Example: 

Python3




# import count method from itertools
from itertools import count 
 
# initialize a list
my_list = [1, 2, 3, 1, 5, 4]
indices = [ind for ind,
           ele in zip(count(),
                      my_list) if ele == 1]
 
# print the indices
print(indices) 


Output:

[0, 3]

Method 4: Using NumPy library

NumPy is general-purpose array-processing package, it provides convenient ways to use arrays in Python. 

Syntax: numpy.where(list_name,value)[index]

where 

  • list_name is the name of the list
  • Value is the value to be searched for
  • Index is the starting index of array (usually it will be “0”)

For this, we will create an array using numpy.array() and then get all the indices of elements in an input array when the condition is satisfied using numpy.where() methods.

Example : 

Python3




# import numpy module
import numpy 
 
# initialize a array
my_list = numpy.array([1, 2, 3, 1, 5, 4]) 
indices = numpy.where(my_list == 1)[0]
 
# display result
print(indices) 


Output :

[0, 3]

The time and space complexity for all methods is same:

Time Complexity: O(n)

Space Complexity: O(n)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!