Open In App

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

Last Updated : 04 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Previous Article
Next Article

Similar Reads

Python - Lambda function to find the smaller value between two elements
The lambda function is an anonymous function. It can have any number of arguments but it can only have one expression. Syntax lambda arguments : expression In this article, we will learn how to find the smaller value between two elements using the Lambda function. Example: Input : 2 5 Output : 2 Input : 7 5 Output : 5Method 1: Using lambda and min(
2 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Difference between List comprehension and Lambda in Python
List comprehension is an elegant way to define and create a list in Python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp. A list comprehension generally consists of these parts : Output expression,Input sequence,A variable representing a member of the input sequence
3 min read
Python Program to Sort the list according to the column using lambda
Given a list, the task is to sort the list according to the column using the lambda approach. Examples: Input : array = [[1, 3, 3], [2, 1, 2], [3, 2, 1]] Output : Sorted array specific to column 0, [[1, 3, 3], [2, 1, 2], [3, 2, 1]] Sorted array specific to column 1, [[2, 1, 2], [3, 2, 1], [1, 3, 3]] Sorted array specific to column 2, [[3, 2, 1], [2
2 min read
Map function and Lambda expression in Python to replace characters
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul We have an existing solution for this problem in C++. Please refer to Replace a character c1 with c2 and c2 with c1 in a string S. We can solve th
2 min read
Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space. Examples: Input : [1, 2, 3, 2, 3, 1, 3] Output : 3 We have existing solution for this problem please refer Find the Number Occurring Odd Number of Times link. we will solv
1 min read
Intersection of two arrays in Python ( Lambda expression and filter function )
Given two arrays, find their intersection. Examples: Input: arr1[] = [1, 3, 4, 5, 7] arr2[] = [2, 3, 5, 6] Output: Intersection : [3, 5] We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function. Implementation: C/C++ Code # Fun
1 min read
Nested Lambda Function in Python
Prerequisites: Python lambda In Python, anonymous function means that a function is without a name. As we already know the def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. When we use lambda function inside another lambda function then it is called Nested Lambda Function. Example 1: #
2 min read
Python: Iterating With Python Lambda
In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python. Syntax: lambda variable : expression Where, variable is used in the expressionexpression can be an mathematical expressio
2 min read
Difference between Normal def defined function and Lambda
In this article, we will discuss the difference between normal def defined function and lambda in Python. Def keyword​​​​​​​In python, def defined functions are commonly used because of their simplicity. The def defined functions do not return anything if not explicitly returned whereas the lambda function does return an object. The def functions m
2 min read
Practice Tags :
three90RightbarBannerImg