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. If however the expression is long we may want to switch to regular functions instead. In this article we will understand how to check if a value exists in a list using the lambda function.

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 which tells whether an element at the index fulfills certain conditions. We need to use the 'list' method of Python to convert it back into the list data type.

Example

In the following example we have used the lambda function to all the elements of the list − 'numbers'. using filter method we have filtered out all the elements which satisfies the condition. Next using the 'list' method we have converted the filtered object into a list.

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)

Output

Value found in the list.
Value not found in the list.

Using ‘Any’ And ‘Map’ Method

'any' is an in−built method in Python. It returns True if at least one element in the iterable satisfies a particular condition. If all the elements do not satisfy the criteria of if the iterable object is empty then it will return False.

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.

Example

In the following example we have used the '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. Next the 'any' method checks if at least one of the elements satisfies the criteria.

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)

Output

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. However this is not an in−built method in Python and we need to import the functool 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. If during any iteration any element returns True then the reduce method would return True immediately and break the iteration.

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)

Output

Value not found in the list.
Value found in the list.

Counting The To Check The Existence

It is very intuitive to understand that if any element exists in a list then it must have total occurance of some non−zero numbers in the list. We therefore just need to count the number of times the element occurred in the list. If it occurs non zero times then it must exist in the list. Although we can use the loop statements like while loop, for loop etc we can also use an in−built method called 'count' 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. Next we have printed our statements depending upon whether the value has non-zero occurrence in our 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)

Output

Value found in the list.
Value not found in the list.

Conclusion

In this article, we understood how to use the lambda function to check if a value exists in a list. We used several other methods like map, filter etc along with the lambda function to achieve this. Additionally, we have also learned how to use the reduce method to check the existence of a value in a list in a cumulative approach.

Updated on: 18-Jul-2023

714 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements