How to Find Index of Occurrences in a Python List

Imagine a situation where you wanted to find every occurrence of something within a Python list. It can seem a little daunting at first. It certainly would be a complex task in many programming languages. But we’ll soon see that it’s surprisingly easy to manage this task in a Python program. The following Python code example shows one of the more basic ways to Python find index of all occurrences in list.

ourList = ["This","is", "a", "list", "of", "words", "to","sort", "as", "a", "list", "example"]
valueIndex = []
for i in range(len(ourList)):
    if ourList[i] == "list":

We begin by creating the ourList variable filled with strings. We’d typically see something like this after converting a sentence original string object into a list. Note that if we use “list” as the given element to find in the particular string example that it’d provide us with two index position results.

In the next line we create an empty list element called valueIndex. The loop beginning on the following line begins by finding the length of the ourList list index and iterating through its range. Every iteration tries to match the original string value against “list”“. If there’s a match then we add the index position to valueIndex. Note that this allows for multiple occurrences of the specified element. Finally, we can print out the results to see what’s contained in valueIndex. We now have the index of every occurrence of the word “list” within the ourList variable.

These multiple indexes point to why we’re not using the standard regular expression Python index method. Index is a concise method to search for a particular element’s index within a list element. However, it only returns the first match. We need to use these more customized solutions to find every occurrence of our search.

We can also use Python list comprehension to find indexes of an item within lists. This is a powerful technique offered by the Python program language. It essentially means that we can use programming logic while defining a given list to create an occurrence count and identify any unique element, duplicate element or elements in your given list index. The following example will show how this can be leveraged while once again using “list” as our default value to index.

ourList = ["This","is", "a", "list", "of", "words", "to","sort", "as", "a", "list", "example"]
valueIndex = [i for i, x in enumerate(ourList) if x == "list"]

We begin by using the same ourList variable from the first example. But this time around we follow it up by creating a list, valueIndex, with list comprehension. The syntax loops and assigns a match based on whether the string Python matches with “list”.

We can also expand on list indexing by using additional libraries. For example, the function selection in NumPy can help us work with more advanced mathematical concepts. The NumPy analog of a list would be the numpy array. The methods used to work with it aren’t too different than a standard list object. Consider the following example.

import numpy as np
ourList = np.array([5, 10, 15, 20, 25, 30, 25])
valueIndex = np.where(ourList == 25)

Of course, we need to begin by actually importing NumPy before we can make use of it. Next, we create a NumPy array as ourList. Note that we’re not using a Python string object or working with sub string values this time. Instead, we’re using a more typical NumPy usage example and working with numbers. NumPy makes it easy to find an occurrence index through use of where. We assign the results of our np.where to valueIndex. Note that we’re only passing one parameter to where. We could also use an end parameter to further refine the results. But for this case we’ll simply search for overlapping occurrences of the number 25. In the next line we print out the results of this function as the 0 position in valueIndex. And lo and behold, it’s the index positions of the number 25.

How to Find Index of Occurrences in a Python List

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top