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 –

1 2 3 |
onelist = [1, 2, 3, 4, 10, 12, 6, 4, 4, 2, 0] |

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

Table of Contents

## 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 –

1 2 3 4 5 6 |
text = 'PYTHON' onelist = [1, 2, 3, 4, 10, 12, 6, 4, 4, 2, 0] print(onelist.count(4)) |

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 –

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
text = 'PYTHON' #define the function that will take the list and element of interest as input def occur(onelist, element): element_indices = [] for i in range(len(onelist)): if onelist[i] == element: element_indices.append(i) return element_indices onelist = [1, 2, 3, 4, 10, 12, 6, 4, 4, 2, 0] element = 4 #print the indices of 4 print(occur(onelist, element)) #print the number of occurrences of 4 print(len(occur(onelist,element))) |

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.

## Further reading:

## 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 –

1 2 3 4 5 6 7 |
text = 'PYTHON' onelist = [1, 2, 3, 4, 10, 12, 6, 4, 4, 2, 0] en_object = enumerate(onelist) print(list(en_object)) |

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 –

1 2 3 4 5 6 7 |
text = 'PYTHON' onelist = [1, 2, 3, 4, 10, 12, 6, 4, 4, 2, 0] el_indices = [index for index, element in enumerate(onelist) if element == 4] print(el_indices) print(len(el_indices)) |

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 –

1234567891011text = 'PYTHON'</li></ul><p>import numpy as nponelist = [1, 2, 3, 4, 10, 12, 6, 4, 4, 2, 0]np_arr = np.array(onelist)el_indices = np.where(np_arr == 4)[0]</p><p>print(el_indices)print(len(el_indices))</p><p>[3, 7, 8] 3In 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 –

1234567891011text = 'PYTHON'from itertools import countonelist = [1, 2, 3, 4, 10, 12, 6, 4, 4, 2, 0]element = 4zipper = [i for i, j in zip(count(), onelist) if j == element]print(zipper)print(len(zipper))[3, 7, 8] 3In 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 –

12345678910text = 'PYTHON'from more_itertools import locateonelist = [1, 2, 3, 4, 10, 12, 6, 4, 4, 2, 0]indices = list(locate(onelist,lambda x: x == 4))print(indices)print(len(indices))[3, 7, 8] 3Here, 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 –

12345678910111213141516171819202122text = 'PYTHON'def get_indices(list_name, element):index_positions = []index = 0while True:try:# Search for an element in the list from index to the endindex = list_name.index(element, index)# Add the indices to the index positions variableindex_positions.append(index)index += 1except ValueError as e:breakreturn index_positionsonelist = [1, 2, 3, 4, 10, 12, 6, 4, 4, 2, 0]indices = get_indices(onelist, 4)print(indices)print(len(indices))[3, 7, 8] 3The 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.