Python Lambda Function to Check if value is in a List (original) (raw)
Last Updated : 04 Jan, 2022
Given a list, the task is to write a Python program to check if the value exists in the list or not using the lambda function.
Example:
Input : L = [1, 2, 3, 4, 5] element = 4 Output : Element is Present in the list
Input : L = [1, 2, 3, 4, 5] element = 8 Output : Element is NOT Present in the list
We can achieve the above functionality using the following two methods.
Method 1: Using Lambda and Count() method
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.
lambda v:l.count(v)
If the count is zero then print the Element is not present else print element is NOT present.
Python3 `
arr = [1, 2, 3, 4] v = 3
def x(arr, v): return arr.count(v)
if(x(arr, v)): print("Element is Present in the list") else: print("Element is Not Present in the list")
`
Output:
Element is Present in the list
Explanation: The arr and v are passed to the lambda function and it evaluates the count of v in the arr and here v = 3 and it is present 1 time in the array and the if condition is satisfied and prints the element Is present in the list.
Method 2: Using Lambda and in keyword
Define a list with integers. Define a lambda function such that it takes array and value as arguments and check if value is present in array using in keyword
lambda v: True if v in l else False
If the lambda return True then print the Element is present else print element is NOT present.
Python3 `
arr=[1,2,3,4] v=8 x=lambda arr,v: True if v in arr else False
if(x(arr,v)): print("Element is Present in the list") else: print("Element is Not Present in the list")
`
Output:
Element is Not Present in the list
Explanation: The arr and v are passed to the lambda function and it checks if the value is present in an array using IN keyword and here v = 8 and it is NOT Present in the array and returns false so prints the element Is NOT present in the list.
Similar Reads
- Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read
- Python List Exercise List OperationsAccess List ItemChange List itemReplace Values in a List in PythonAppend Items to a listInsert Items to a listExtend Items to a listRemove Item from a listClear entire listBasic List programsMaximum of two numbersWays to find length of listMinimum of two numbersTo interchange first an 3 min read
- Python String Exercise Basic String ProgramsCheck whether the string is Symmetrical or PalindromeFind length of StringReverse words in a given StringRemove i’th character from stringAvoid Spaces in string lengthPrint even length words in a stringUppercase Half StringCapitalize the first and last character of each word in 4 min read
- Python Tuple Exercise Basic Tuple ProgramsPython program to Find the size of a TuplePython – Maximum and Minimum K elements in TupleCreate a list of tuples from given list having number and its cube in each tuplePython – Adding Tuple to List and vice – versaPython – Sum of tuple elementsPython – Modulo of tuple elementsP 3 min read
- Python Dictionary Exercise Basic Dictionary ProgramsPython | Sort Python Dictionaries by Key or ValueHandling missing keys in Python dictionariesPython dictionary with keys having multiple inputsPython program to find the sum of all items in a dictionaryPython program to find the size of a DictionaryWays to sort list of dicti 3 min read
- Python Set Exercise Basic Set ProgramsFind the size of a Set in PythonIterate over a set in PythonPython - Maximum and Minimum in a SetPython - Remove items from SetPython - Check if two lists have at-least one element commonPython program to find common elements in three lists using setsPython - Find missing and addit 2 min read