Find the index of elements that meet a condition in Python

avatar

Borislav Hadzhiev

Last updated: Sep 23, 2022

banner

Photo from Unsplash

Find the index of elements that meet a condition in Python #

To find the index of the elements that meet a condition:

  1. Use a list comprehension to iterate over a range object.
  2. Check if a condition is met and return the corresponding index if it is.
  3. The new list will only contain the indexes of the elements that meet the condition.
main.py
my_list = [4, 8, 12, 16, 25] indexes = [ index for index in range(len(my_list)) if my_list[index] > 10 ] print(indexes) # 👉️ [2, 3, 4]

We used a list comprehension to iterate over a range object containing the indexes in the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

The range class is commonly used for looping a specific number of times.

main.py
my_list = [4, 8, 12, 16, 25] print(list(range(len(my_list)))) # 👉️ [0, 1, 2, 3, 4] print(list(range(5))) # 👉️ [0, 1, 2, 3, 4]

We used the list's length to construct a range object that contains all of the indexes in the list.

On each iteration, we check if the current list item is greater than 10 and return the result.

main.py
my_list = [4, 8, 12, 16, 25] indexes = [ index for index in range(len(my_list)) if my_list[index] > 10 ] print(indexes) # 👉️ [2, 3, 4]

The new list only contains the indexes of the elements that meet the condition.

You can use this approach to get the indexes of the elements that meet any condition in the list.

main.py
my_list = ['bobby', 'ab', 'cd', 'bobbyhadz.com'] indexes = [ index for index in range(len(my_list)) if my_list[index].startswith('bobby') ] print(indexes) # 👉️ [0, 3]

The example finds the indexes of the elements that start with a specific substring.

If you use numpy, you can also use the numpy.where() method.

main.py
import numpy as np my_list = np.array([4, 8, 12, 16, 25]) indexes = np.where(my_list > 10)[0] print(indexes) # 👉️ [2 3 4] indexes = np.nonzero(my_list > 10)[0] print(indexes) # 👉️ [2 3 4]

When only a condition is provided, the numpy.where method returns the indices of the elements that meet the condition.

Alternatively, you can use a for loop.

Find the index of elements that meet a condition using a for loop #

To find the index of the elements that meet a condition:

  1. Declare a new variable that stores an empty list.
  2. Use a for loop to iterate over the original list with enumerate().
  3. Check if a condition is met on each iteration.
  4. Append the matching indexes to the new list.
main.py
my_list = [4, 8, 12, 16, 25] indexes = [] for index, item in enumerate(my_list): if item > 10: indexes.append(index) print(indexes) # 👉️ [2, 3, 4]

We used the enumerate() function to get access to the index of the current iteration.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 bobby, 1 hadz, 2 com

On each iteration, we check if the current element meets a condition.

If the condition is met, we use the list.append() method to append the current index to the new list.

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # 👉️ ['bobby', 'hadz', 'com']

The method returns None as it mutates the original list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.