Get index of first element in List that matches a condition

In this article, we will discuss the different ways to find the index position of first element in a List that matches a given condition.

Table Of Contents

Introduction

Suppose we have a list of numbers,

listOfNumbers = [33, 45, 26, 12, 11, 76, 6, 14]

This list have many elements which are less than 15, but we want to find the index position of first element that is less than 15. Like, in this list first element that is less than 15 is 12, and its index position is,

3

Method 1: Using enumerate() and for loop

Pass a list to the enumerate() function, and get a list of pairs. Each pair contains an index position, and value at that index positions. Iterate over all the pairs returned by the enumerate() function, and for each pair check if the value matches the given condition or not. As soon as, you find a value that matches the condition, store the index position, and break the loop.

Advertisements

Example 1:

Let’s use the above logic to find the index position of first number in list that is less than 15.

# List of numbers
listOfNumbers = [33, 45, 26, 12, 11, 76, 6, 14]

# Get index of first element that is less than 15
firstIndex = -1

# Iterate overa ll values of list along with index position
for index, value in enumerate(listOfNumbers):
    # If value is less than 15, then break the loop
    # and store the corresponding index position
    if value < 15:
        firstIndex = index
        break

print(firstIndex)

Output:

3

It printed the index position of first element in the list, that was less than 15.

Example 2:

If we try to look with a condition, that no element in the list statisfies, then it will return -1. For example,

# List of numbers
listOfNumbers = [33, 45, 26, 12, 11, 76, 6, 14]

# Get index of first element that is less than 15
firstIndex = -1

# Iterate overa ll values of list along with index position
for index, value in enumerate(listOfNumbers):
    # If value is less than 5, then break the loop
    # and store the corresponding index position
    if value < 5:
        firstIndex = index
        break

print(firstIndex)

Output:

-1

There was no number less than 5 in the list. Therefore, it returned -1.

Method 2: Using next()

Pass a list to the enumerate() function, and get a list of pairs. Each pair contains an index position, and value at that index positions. Iterate over all the pairs returned by the enumerate() function, and for each pair check if the value matches the given condition or not. As soon as you find the element, that statisfies the condition break the loop.

But this time do all this in a generator expression, and pass that next() function, along with default value -1.

Example 1:

Let’s use the above logic to find the index position of first number in list that is less than 15.

# List of numbers
listOfNumbers = [33, 45, 26, 12, 11, 76, 6, 14]

# Get index of first element that is less than 15
firstIndex = next((index for index, value in enumerate(listOfNumbers) if value < 15), -1)

print(firstIndex)

Output:

3

It printed the index position of first element in the list, that was less than 15.

Example 2:

If we try to look with a condition, that no element in the list statisfies, then it will return -1. For example,

# List of numbers
listOfNumbers = [33, 45, 26, 12, 11, 76, 6, 14]

# Get index of first element that is less than 5
firstIndex = next((index for index, value in enumerate(listOfNumbers) if value < 5), -1)

print(firstIndex)

Output:

-1

There was no number less than 5 in the list. Therefore, it returned -1.

Summary

We learned how to get the index of first element in List that matches a condition. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top