Python - Lambda Function to Check if a value is in a List

Lambda functions in Python are nameless functions which are useful when we want to combine multiple expressions in a single line. The lambda function is convenient when we are sure that we won't be reusing the code anywhere else in the program. In this article we will understand how to check if a value exists in a list using the lambda function combined with various built-in methods.

Using The filter() Method

The filter() method in Python is a built-in function which creates new elements in the list depending upon certain filter conditions. It returns a filter object which has the boolean mask that tells whether an element at the index fulfills certain conditions. We need to use the list() method to convert it back into the list data type.

Example

In the following example we have used the lambda function on all the elements of the list. Using filter() method we have filtered out all the elements which satisfy the condition

def does_exists(numbers, value):
    result = list(filter(lambda x: x == value, numbers))
    if result:
        print("Value found in the list.")
    else:
        print("Value not found in the list.")

numbers = [1, 2, 3, 4, 5]
value = 3
does_exists(numbers, value)

value = 45
does_exists(numbers, value)
Value found in the list.
Value not found in the list.

Using any() and map() Methods

any() is a built-in method in Python that returns True if at least one element in the iterable satisfies a particular condition. If all the elements do not satisfy the criteria or if the iterable object is empty then it will return False.

map() method is a built-in 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 parameters.

Example

In the following example we have used any(), map(), and the lambda function to check if a value exists in a list. We used the map() method to apply lambda function to all the elements of the list

def does_exists(numbers, value):
    result = any(map(lambda x: x == value, numbers))
    if result:
        print("Value found in the list.")
    else:
        print("Value not found in the list.")

numbers = [1, 2, 3, 4, 5]
value = 3
does_exists(numbers, value)

value = 45
does_exists(numbers, value)
Value found in the list.
Value not found in the list.

Using The reduce() Method

The reduce() method in Python allows the user to perform some cumulative computations to any iterable objects by using some specific functions. This is not a built-in method in Python and we need to import the functools library to use this.

Example

In the following example we have used the lambda and the reduce() method of Python. Using the lambda function we have checked if the value of the variable acc or x is equal to the desired value for all the elements of the list

from functools import reduce

def does_exists(numbers, value):
    result = reduce(lambda acc, x: acc or x == value, numbers, False)
    if result:
        print("Value found in the list.")
    else:
        print("Value not found in the list.")

numbers = [1, 2, 37, 4, 5]
value = 378
does_exists(numbers, value)

value = 37
does_exists(numbers, value)
Value not found in the list.
Value found in the list.

Using count() Method to Check Existence

If any element exists in a list then it must have total occurrence of some non-zero numbers in the list. We therefore just need to count the number of times the element occurred in the list. We can use the built-in count() method which returns the occurrences of the element in a list.

Example

In the following example we used the count() method on the list. We passed the desired value as the argument to the method. The method returned the frequency of the element in the list

def does_exists(numbers, value):
    result = numbers.count(value)
    if result:
        print("Value found in the list.")
    else:
        print("Value not found in the list.")

numbers = [1, 26, 37, 4, 5]
value = 26
does_exists(numbers, value)

value = 378
does_exists(numbers, value)
Value found in the list.
Value not found in the list.

Comparison

Method Uses Lambda Performance Best For
filter() Yes Moderate Getting all matching elements
any() + map() Yes Good Quick boolean check
reduce() Yes Moderate Complex cumulative operations
count() No Best Simple existence check

Conclusion

In this article, we understood how to use the lambda function to check if a value exists in a list using various methods like filter(), map(), and reduce(). The any() + map() approach provides the most efficient lambda-based solution for checking existence in a list.

Updated on: 2026-03-27T08:34:31+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements