How to find index of all occurrences in list in Python

List is a data type in Python. It is used to store multiple values in one variable. If you want to find out how many times a certain element shows up in a list and where – you’ve come to the right place.

In this tutorial, we’ll show you how to find index of all occurrences in list in Python. Let’s get started.
Imagine we have a python list –

Let’s count the occurrences for this list. We’ll be showing you seven methods that you can use. They are –

The easiest method – Using list count() method

The count() method will return the occurrences of any element in a list. Let’s find out how many times 4 occurs in our list with this method. Here’s the code –

3

This is the easiest method to count the occurrences. However, this method does not provide the indices of the specified element.
The following methods will show you the count as well as the indices of where those specific elements are.

The Basic Method – Using for loops

You can just write a for loop that will find out the occurrences for a specified element in a list. Let’s take a look at the following code –

[3, 7, 8] 3

In this piece of code, we defined a function that takes a list and the element as input and returns the indices of that specific element. The for loop inside the function checks each of the items in the list and registers the indices where the specified item/element is. After that, we can just find the number of occurrences using the len() function like in the code.

The shortest method – Using the enumerate() function

The enumerate() function in python adds an index number to any iterable and returns an object. Let’s see it in action by looking at the object which enumerate() function returns –

[(0, 1), (1, 2), (2, 3), (3, 4), (4, 10), (5, 12), (6, 6), (7, 4), (8, 4), (9, 2), (10, 0)]

As you can see, we converted the enumerate object into a list and printed it. The output is in the format of (index, element). This is how the enumerate() function added an index to each element of the original list.
Now we’ll show you a one-line code to find out the occurrences. We’ll use list comprehension with the enumerate() function –

[3, 7, 8] 3

Here, enumerate(onelist) returned pairs (index, element) and we looped through all of those pairs, filtered the element of our interest, and stored the indices of those elements.

The fastest method – Using a numpy array

For this method, you will need the numpy library. Numpy arrays are better than lists mainly because of the following reasons –

  • Numpy arrays only contain a single data type while the lists may contain different data types. This makes the numpy arrays smaller in size and more optimized than the lists.
  • Numpy arrays are faster than lists.
  • Numpy arrays have much better and more optimized functionality than lists.
    Now that you know why Numpy arrays are better, let’s dive into the program using the numpy array –
    [3, 7, 8] 3

    In this piece of code, we imported the numpy library at first. Then we converted our list into a numpy array. The numpy where() method returns the indices when some certain condition is met. In our case, we set the condition to be equal to 4 and got the indices of 4 in the array. After that, we just simply print out the indices and the number of occurrences with the len() function.

    Using itertools module

    The itertools library includes some efficient iterators. The count() function is an infinite iterator. We used the zip() function to join two lists together. Let’s see the code in action –

    [3, 7, 8] 3

    In the code, we iterate through the list, and the zip() function stitches each element and their indices that count() outputs. We set the zipper variable to only include the indices of the element of our interest using the if statement.

    Using more-itertools Library

    We can also use the locate() function in the more-itertools library. The locate() function returns the indices of an iterable where some conditions are met. Keep in mind, you will require the more_itertools library before you can run this code. Let’s jump into the code –

    [3, 7, 8] 3

    Here, the locate() function iterates through the list and stores the indices when it encounters element 4.

    Using index() function

    In this method, we’ll use the index() function. The index() function outputs the lowest index of an element. However, we can code to get the indices of all the occurrences. Here is the code –

    [3, 7, 8] 3

    The index function takes in inputs of where to start the countdown from. The syntax is listname.index(element, start, end). In the above piece of code, we increased the start value for the index() function and got the output of the indices of 4.

    That’s all about find index of all occurrences in list in Python.

    Was this post helpful?

Related Posts

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.ConclusionWas this post helpful? In this […]

  • 19 May

    Compare list elements with each other in Python

    Table of ContentsCompare all elements in list PythonWays to compare list elements with each other in PythonUsing the for loop to compare all elements in list PythonUsing the itertools.combinations() function to compare all elements in list PythonConclusionWas this post helpful? In this post, we will see how to compare list elements with each other in […]

  • 18 May

    How to Deep Copy a List in Python

    Table of ContentsWhat is a list in Python?What are shallow copy and deep copy operations in Python?Shallow CopyDeep CopyHow to deep copy a list in Python?Using the deepcopy() function from the copy module to deep copy a list in Python.Using a user-defined function to deep copy a list in Python.Using list comprehension to deep copy […]

  • 18 May

    Convert Generator to List in Python

    Table of ContentsLists and Generators in PythonWays to convert generator to list in PythonUsing the list() function to convert generator to list in PythonUsing the unpack operator * to convert generator to list in PythonUsing the list comprehension to convert generator to list in PythonUsing the append() function to convert generator to list in PythonConclusionWas […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusionWas this […]

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusionWas this post helpful? In […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.