Get Last element in List that matches condition in Python

This tutorial will discuss about unique ways to get last element of a List, that matches a given condition in Python.

Table Of Contents

Introduction

Suppose we have a list of numbers, and we want to get the last odd number from that list. Basically, we want to fetch the last item from list that satisfies our 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

First get a copy of the list with the reversed contents. Then use a Generator expression on this reversed list, 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. But that will be the last odd number from original 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 last odd number from list.

Let’s see the complete example,

listOfnumbers = [12, 42, 44, 68, 91, 72, 71, 81, 82]

# Get last item in list that matches a condition e.g.
# Get last odd value from the list
value = next( (elem for elem in reversed(listOfnumbers) if elem % 2 == 1), None)

print(value)

Output

81

Method 2: Using for-loop

First get a copy of the list with the reversed contents. Then iterate over this reversed 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 the reversed list. But that will be the last odd value from the original list.

Let’s see the complete example,

listOfnumbers = [12, 42, 44, 68, 91, 72, 71, 81, 82]

value = None

# Get last item in list that matches a condition e.g.
# Get last odd value from the list
for elem in reversed(listOfnumbers) :
    if elem % 2 == 1:
        value = elem
        break

print (value)

Output

81

Method 3: Using any() method

First get a copy of the list with the reversed contents. Then iterate over all elements of reversed List in a generator expression, and check the given condition on each element. During iteration, store each element in variable value. Pass this generator expression into the any() function. As soon as any element satisfies the
Condition in generator expression, it will yield a True value. 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 reversed list, and that will be the last odd number from the original 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 last item in list that matches a condition e.g.
# Get last odd value from the list
if not any((value := elem) % 2 == 1 for elem in reversed(listOfnumbers)):
    value = None

print(value)

Output

81

Summary

We learned about different ways to access the last element in list that matches the given condition in Python.

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