You can use the filter method:

>>> lst = [1, 2, 3, 4, 5]
>>> filter(lambda x: x % 2 == 0, lst)
[2, 4]

or a list comprehension:

>>> lst = [1, 2, 3, 4, 5]
>>> [x for x in lst if x %2 == 0]
[2, 4]

to find a single element, you could try:

>>> next(x for x in lst if x % 2 == 0)
2

Though that would throw an exception if nothing matches, so you'd probably want to wrap it in a try/catch. The () brackets make this a generator expression rather than a list comprehension.

Personally though I'd just use the regular filter/comprehension and take the first element (if there is one).

These raise an exception if nothing is found

filter(lambda x: x % 2 == 0, lst)[0]
[x for x in lst if x %2 == 0][0]

These return empty lists

filter(lambda x: x % 2 == 0, lst)[:1]
[x for x in lst if x %2 == 0][:1]
Answer from John Montgomery on Stack Overflow
Top answer
1 of 2
135

You can use the filter method:

>>> lst = [1, 2, 3, 4, 5]
>>> filter(lambda x: x % 2 == 0, lst)
[2, 4]

or a list comprehension:

>>> lst = [1, 2, 3, 4, 5]
>>> [x for x in lst if x %2 == 0]
[2, 4]

to find a single element, you could try:

>>> next(x for x in lst if x % 2 == 0)
2

Though that would throw an exception if nothing matches, so you'd probably want to wrap it in a try/catch. The () brackets make this a generator expression rather than a list comprehension.

Personally though I'd just use the regular filter/comprehension and take the first element (if there is one).

These raise an exception if nothing is found

filter(lambda x: x % 2 == 0, lst)[0]
[x for x in lst if x %2 == 0][0]

These return empty lists

filter(lambda x: x % 2 == 0, lst)[:1]
[x for x in lst if x %2 == 0][:1]
2 of 2
11

Generators and list comprehensions are more pythonic than chainable functions.

>>> lst = [i for i in range(1, 6)]

>>> lst
[1, 2, 3, 4, 5]

>>> gen = (x for x in lst if x % 10 == 0)

>>> next(gen, 'not_found')
'not_found'

>>> [x for x in gen]
[]

For example, I use it like this sometimes:

>>> n = next((x for x in lst if x % 10 == 0), None)
>>> if n is None:
...     print('Not found')
... 
Not found

Otherwise, you can define your utility function oneliners like this:

>>> find = lambda fun, lst: next((x for x in lst if fun(x)), None)
>>> find(lambda x: x % 10 == 0, lst)
>>> find(lambda x: x % 5 == 0, lst)
5

>>> findall = lambda fun, lst: [x for x in lst if fun(x)]
>>> findall(lambda x: x % 5 == 0, lst)
[5]
Discussions

How to return a list of lambda function?
That's because your lambda function includes the variable i that it gets from the environment. Trouble is, all the lambda functions share the same environment so for each function i has the last value assigned to it which is 3. One way around the problem is to force the i value to be passed as a parameter to the lambda which "preserves" the i value as the value when the lambda was actually defined: list1 = [1, 2, 3] pwd = [lambda x, i=i: x**i for i in list1] print(pwd) for func in pwd: print(func(2)) More on reddit.com
🌐 r/learnpython
19
87
December 2, 2022
How can I find a value in a list using Python? - Ask a Question - TestMu AI (formerly LambdaTest) Community
I currently use the following code to check if an item is in my_list: if item in my_list: print("Desired item is in the list") Is using if item in my_list: the most “pythonic” way to find an item in a list? More on community.testmuai.com
🌐 community.testmuai.com
0
June 3, 2024
Python: Fetch item in list where dict key is some value using lambda - Stack Overflow
Is it possible to fetch using lambda? I know that we can do sorted function with lambda and its VERY useful. Is there a short form way of fetching an object in a list in which the object at key 'i... More on stackoverflow.com
🌐 stackoverflow.com
python - Using lambda function to find element from another list or array - Stack Overflow
lambda is a function by itself. Putting it into a def formal function as the sole output make no sense. If you want to multiply by 2 for an entire list, lambda will not be enough, you'll also need an iterator. More on stackoverflow.com
🌐 stackoverflow.com
🌐
TutorialsPoint
tutorialspoint.com › python-lambda-function-to-check-if-a-value-is-in-a-list
Python - Lambda Function to Check if a value is in a List
July 18, 2023 - Map method on the other hand is an in?built method in Python which allows us to apply any specific function to all the elements of the iterable object. It takes the function and the iterable object as the parameters. In the following example we have used the 'any', 'map', and the 'lambda' function to check if a value exists in a list.
🌐
w3resource
w3resource.com › python-exercises › lambda › python-lambda-exercise-40.php
Python: Find the nested lists elements, which are present in another list using lambda - w3resource
Python Exercises, Practice and Solution: Write a Python program to find the nested list elements, which are present in another list using lambda.
🌐
w3resource
w3resource.com › python-exercises › lambda › python-lambda-exercise-39.php
Python: Find the elements of a given list of strings that contain specific substring using lambda - w3resource
July 12, 2025 - Python Exercises, Practice and Solution: Write a Python program to find the elements of a given list of strings that contain a specific substring using lambda.
🌐
Quora
quora.com › How-do-you-approach-list-comprehension-with-lambda-Python-lambda-list-comprehension-development
How to approach list comprehension with lambda (Python, lambda, list comprehension, development) - Quora
Answer: I usually approach it by not trying to use them together in the first place. One of the points of a comprehension is that you can just use an expression directly for the transformed value or the test, unlike a map or filter call. For example, these all do the same thing: [code]>>> valu...
🌐
CodeRivers
coderivers.org › blog › python-list-find-by-lambda
Python List Find by Lambda: A Comprehensive Guide - CodeRivers
February 22, 2026 - The `lambda` function, a small anonymous function, can be a powerful tool in this regard. By combining lists and `lambda` functions, we can perform efficient and concise searches. This blog post will explore how to use `lambda` to find elements in Python lists, covering basic concepts, usage ...
🌐
GeeksforGeeks
geeksforgeeks.org › lambda-filter-python-examples
Lambda and filter in Python Examples - GeeksforGeeks
April 8, 2025 - Before diving into examples, let’s quickly understand the function of lambda and filter() in Python: lambda function: A small, anonymous function defined using the lambda keyword. It can take any number of arguments but has only one expression. filter() function: A built-in function that filters elements from an iterable based on a condition (function) that returns True or False. For example, suppose we want to filter even numbers from a list, here's how we can do it using filter() and lambda:
Find elsewhere
🌐
Google Groups
groups.google.com › g › vim_dev › c › Ks5Jq3Ux-1s
Finding an item using a lambda function in a List using the index() function
Or, to make it more like map() and filter(), the second argument would be the lambda/funcref and the third argument a dict with optional arguments. That's probably best, since the only optional argument that makes sense is the start index. Not sure what to call it, indexfunc() ? -- ARTHUR: Who are you? TALL KNIGHT: We are the Knights Who Say "Ni"! BEDEVERE: No! Not the Knights Who Say "Ni"! "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD /// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///
🌐
Reddit
reddit.com › r/learnpython › how to return a list of lambda function?
How to return a list of lambda function? : r/learnpython
December 2, 2022 - If you really want to construct a list of function objects, you'll want to use closures: nums = [1, 2, 3] pwd = [(lambda y: lambda x: x**y)(num) for num in nums] This would give you the results you're expecting, because each function is now actually getting a separate value.
🌐
w3resource
w3resource.com › python-exercises › lambda › python-lambda-exercise-26.php
Python: Find the list with maximum and minimum length using lambda - w3resource
# Define a function 'max_length_list' that takes a list of lists 'input_list' as input def max_length_list(input_list): # Calculate the maximum length of the sublists in 'input_list' max_length = max(len(x) for x in input_list) # Find the sublist with the maximum length using the 'max' function and a lambda function max_list = max(input_list, key=lambda i: len(i)) # Return a tuple containing the maximum length and the sublist with the maximum length return (max_length, max_list) # Define a function 'min_length_list' that takes a list of lists 'input_list' as input def min_length_list(input_lis
🌐
TestMu AI
community.testmuai.com › ask a question
How can I find a value in a list using Python? - Ask a Question - TestMu AI (formerly LambdaTest) Community
June 3, 2024 - I currently use the following code to check if an item is in my_list: if item in my_list: print("Desired item is in the list") Is using if item in my_list: the most “pythonic” way to find an item in a list?
Top answer
1 of 4
10

Using lambda here isn't necessary. Lambda isn't something magical, it's just a shorthand for writing a simple function. It's less powerful than an ordinary way of writing a function, not more. (That's not to say sometimes it isn't very handy, just that it doesn't have superpowers.)

Anyway, you can use a generator expression with the default argument. Note that here I'm returning the object itself, not 20, because that makes more sense to me.

>>> somelist = [{"id": 10, "x": 1}, {"id": 20, "y": 2}, {"id": 30, "z": 3}]
>>> desired_val = next((item for item in somelist if item['id'] == 20), None)
>>> print(desired_val)
{'y': 2, 'id': 20}
>>> desired_val = next((item for item in somelist if item['id'] == 21), None)
>>> print(desired_val)
None
2 of 4
7

Using a lambda as you asked, with a generator expression, which is generally considered more readable than filter, and note this works equally well in Python 2 or 3.

lambda x: next(i for i in x if i['id'] == 20)

Usage:

>>> foo = lambda x: next(i for i in x if i['id'] == 20)
>>> foo(x)
{'Car': 'Toyota', 'id': 20}

And this usage of lambda is probably not very useful. We can define a function just as easily:

def foo(x):
    return next(i for i in x if i['id'] == 20)

But we can give it docstrings, and it knows its own name and has other interesting attributes that anonymous functions (that we then name) don't have.

Additionally, I really think what you're getting at is the filter part of the expression.

In

filter(lambda x: x[id]==20, x)

we have replaced that functionality with the conditional part of the generator expression. The functional part of generator expressions (list comprehensions when in square brackets) are similarly replacing map.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-function-to-check-if-value-is-in-a-list
Python - Lambda Function to Check if value is in a List - GeeksforGeeks
July 23, 2025 - Define a list with integers. Define a lambda function such that it takes array and value as arguments and return the count of value in list.
🌐
IncludeHelp
includehelp.com › python › check-if-value-is-in-a-list-using-lambda-function.aspx
Check if value is in a List using Lambda Function in Python
March 14, 2022 - There is an integer list, define a lambda function that will take the list and will return the count of the given element.
🌐
w3resource
w3resource.com › python-exercises › lambda › python-lambda-exercise-46.php
Python: Find all index positions of the maximum and minimum values in a given list using lambda - w3resource
July 12, 2025 - Write a Python program to return the index and value of the first occurrence of the minimum value in a list using lambda. Write a Python program to find the indices of both the maximum and minimum values in a list and swap them using lambda.
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
How can I write a lambda to check for a particular word in a list? - Python FAQ - Codecademy Forums
June 26, 2018 - Answer If we’re given some list of strings, like languages = ["HTML", "JavaScript", "Python", "Ruby"], we’d write a lambda where each value checked is each item in the list. From the example below, we see x being used as each number in a ...
🌐
Finxter
blog.finxter.com › how-to-filter-in-python-using-lambda-functions
How to Filter in Python using Lambda Functions? – Be on the Right Side of Change
The second argument is the iterable to be filtered—the lambda function checks for each element in the iterable whether the element pass the filter or not. The filter() function returns an iterator with the elements that pass the filtering condition. lst = [1, 2, 3, 4, 5] # Filter all elements <3 my_list = filter(lambda x: x<3, lst) print(list(my_list)) # [1, 2]
🌐
Studytonight
studytonight.com › post › find-a-given-elements-index-in-python-list
Find a given element's index in Python List - Studytonight
August 31, 2021 - In the code above, the zip function takes iterable attributes and the lambda function, which is an anonymous function (a function that doesn’t have a name to it), takes the arguments and matches the value that we search for. It returns the index of the element that is being searched in the list. There are many different ways to accomplish the solve a problem in python.