This tutorial will discuss about unique ways to get first element in list that matches condition in Python.
Table Of Contents
Introduction
We have a list of numbers, and we want to get the first odd number from that list. Basically, we want to fetch the first element from list that satisfies out condition.
The condition is :
* Number should be Odd i.e. if we divide it by two then reminder should be 1,
Method 1: Using next() method
Use a Generator expression to yield only those elements from list that satisfies a given condition i.e. only odd numbers. Basically, it will return us a generator object. Then pass that generator object to the next()
function in Python, along with a default value None. The next()
method, will return the first item from generator object i.e. first odd number from list. If there is no item in list that satisfies the given condition i.e. no odd number in list, then it will return None
. So, in the below example, either it will return None
or the first odd number
from list.
Let’s see the complete example,
listOfNumbers = [12, 42, 44, 68, 91, 72, 71, 81, 82] # Get first item in list that matches a condition e.g. # Get first odd value from the list value = next( (elem for elem in listOfNumbers if elem % 2 == 1), None) print(value)
Output
Frequently Asked:
91
Method 2: Using for-loop
Iterate over the list using a for-loop, and for each element check if satisfies the given condition or not. If yes, then stop the loop and return the current value.
In the below example, our condition is that the number should be odd. So, it will return the first odd number from list.
Let’s see the complete example,
listOfNumbers = [12, 42, 44, 68, 91, 72, 71, 81, 82] value = None # Get first item in list that matches a condition e.g. # get first odd value from the list for elem in listOfNumbers: if elem % 2 == 1: value = elem break print (value)
Output
91
Method 3: Using any() method
The any()
method accepts an iterable sequence as an argument and reurns True
as soon as any element in that sequence evaluates to True.
So, iterate over all elements of List in a generator expression, and check the given condition on each element. While iterating store each element in variable “value”. As soon as any element satisfies the condition, it will evaluate to True
for that element. The any()
method will return True at that point. The variable “value” will have the value for which any() method returned True
.
In the below example, our condition is that the number should be a odd number. So, it will fetch the first odd number from list, or if list does not have any odd number then it will give None
.
Let’s see the complete example,
listOfNumbers = [12, 42, 44, 68, 91, 72, 71, 81, 82] # Get first item in list that matches a condition e.g. # get first odd value from the list if not any((value := elem) % 2 == 1 for elem in listOfNumbers): value = None print (value)
Output
91
Summary
We learned about different ways to access the first element in list that matches the given condition in Python.