Javatpoint Logo
Javatpoint Logo

How to Get Indices of All Occurrences of an Element in Python?

In this tutorial, we will explore how to use various Python functions to locate the index of every instance of an element in a given list. Finding an element's initial index in the list is frequently made simple by Python. Finding an object's all the list indices can be useful since Python lists may include duplicate objects.

How to use Python to determine the indices of every instance of an item in a list:

  • For loop and enumerate,
  • more_itertools,
  • numpy,
  • List comprehensions,
  • itertools

With a For Loop and Enumerate

Using a Python for loop along with the Python enumerate tool can be one of the simplest ways to obtain the index list of all instances of an item in the given Python list. When iterating through an object, the enumerate method returns the element and the element's index.

As a result, we can determine if the element equals the one we're looking for. If so, we can include the item's position in a different list. Let's use an illustration to understand how this operates:

Code

Output

The element is present at these indices:  [0, 3, 7, 9]

With more_itertools

There are several useful functions included in the more_itertools library. The locate() method, which takes a Python iterable data structure and a built-in or user-defined function to compare against, is one of them.

We can employ an anonymous function called the lambda function that determines whether an item is equal to or matches the item we want to compare to obtain the index locations of all items that match the given element.

Let's understand this with an illustration:

Code

Output

The element is present at these indices:  [0, 3, 7, 9]

With Numpy

Obtaining all of an item's index locations in a list is a simple and quick procedure made possible by NumPy. We can use the where() method for this. The index values of each item in the given array that matches a specified value are returned by the where() method.

Let's understand this with an illustration:

Code

Output

The element is present at these indices:  [0, 3, 7, 9]

With a List Comprehension

In this part, we'll look at using Python list comprehension to get a list of indices for the given element in a list. Except for using a list comprehension, this approach functions identically to the loop-based first method.

Let's look at how we can change the previously made for loop into a Python list comprehension:

Code

Output

The element is present at these indices:  [0, 3, 7, 9]

Itertools Module

We will utilize the count() functions from the itertools library because they are memory-efficient utilities that we may use separately or in combination. They will produce an iterator of equally spaced values starting with the start integer. We will first construct a list for this purpose, and then, using list comprehension and zip(), we will obtain the indexes of every instance of an item in the list.

Code

Output

The element is present at these indices:  [0, 3, 7, 9]






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA