Skip to content
Related Articles

Related Articles

Python lambda

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 31 Aug, 2022
View Discussion
Improve Article
Save Article

In Python, an anonymous function means that a function is without a name. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions.

Python lambda Syntax:

lambda arguments : expression

Python lambda Example:

Python3




calc = lambda num: "Even number" if num % 2 == 0 else "Odd number"
 
print(calc(20))


Output:

Even number

Python lambda properties:

  • This function can have any number of arguments but only one expression, which is evaluated and returned.
  • One is free to use lambda functions wherever function objects are required.
  • You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.
  • It has various uses in particular fields of programming, besides other types of expressions in functions.

Example 1: Program to demonstrate return type of Python lambda keyword 

Python3




string = 'GeeksforGeeks'
 
# lambda returns a function object
print(lambda string: string)


Output

<function <lambda> at 0x7fd7517ade18>

Explanation: In this above example, the lambda is not being called by the print function, but simply returning the function object and the memory location where it is stored. So, to make the print to print the string first, we need to call the lambda so that the string will get pass the print.

Example 2: Invoking lambda return value to perform various operations

Here we have passed various types of arguments into the different lambda functions and printing the result generated from the lambda function calls.

Python3




filter_nums = lambda s: ''.join([ch for ch in s if not ch.isdigit()])
print("filter_nums():", filter_nums("Geeks101"))
 
do_exclaim = lambda s: s + '!'
print("do_exclaim():", do_exclaim("I am tired"))
 
find_sum = lambda n: sum([int(x) for x in str(n)])
print("find_sum():", find_sum(101))


Output:

filter_nums(): Geeks
do_exclaim(): I am tired!
find_sum(): 2

Example 3: Difference between lambda and normal function call

The main difference between lambda function and other functions defined using def keyword is that, we cannot use multiple statements inside a lambda function and allowed statements are also very limited inside lambda statements. Using lambda functions to do complex operations may affect the readability of the code.

Python3




def cube(y):
    print(f"Finding cube of number:{y}")
    return y * y * y
 
 
lambda_cube = lambda num: num ** 3
 
# invoking simple function
print("invoking function defined with def keyword:")
print(cube(30))
# invoking lambda function
print("invoking lambda function:", lambda_cube(30))


Output:

invoking function defined with def keyword:
Finding cube of number:30
27000
invoking lambda function: 27000

Example 4: The lambda function gets more helpful when used inside a function.

We can use lambda function inside map(), filter(), sorted() and many other functions. Here, we have demonstrated how to use lambda function inside some of the most common Python functions.

Python3




l = ["1", "2", "9", "0", "-1", "-2"]
# sort list[str] numerically using sorted()
# and custom sorting key using lambda
print("Sorted numerically:",
      sorted(l, key=lambda x: int(x)))
 
# filter positive even numbers
# using filter() and lambda function
print("Filtered positive even numbers:",
      list(filter(lambda x: not (int(x) % 2 == 0 and int(x) > 0), l)))
 
# added 10 to each item after type and
# casting to int, then convert items to string again
print("Operation on each item using lambda and map()",
      list(map(lambda x: str(int(x) + 10), l)))


Output:

Sorted numerically: ['-2', '-1', '0', '1', '2', '9']
Filtered positive even numbers: ['1', '9', '0', '-1', '-2']
Operation on each item using lambda and map() ['11', '12', '19', '10', '9', '8']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!