Before now, we know that in Python you defined a function by a name. However, today, we’ll be looking at an “anonymous Function” that can be defined without a name. In three minutes read, you’ll have complete knowledge of the Python Lambda Functions, and your knowledge won’t be limited to:
- What is Python Lambda?
- Characteristics of lambda
- Uses of the lambda
- Lambda with map
- Lambda with filter
- Lambda with reduce
- Operations on lambda
- Python Nested Lambda
Afterward, you’ll be thankful to put your knowledge to work as I summarize.
What is Python Lambda?
Python Lambda is an anonymous function in Python that performs like regular python Functions. They’re anonymous because they can be defined without a name. A striking difference between the python Lambda Functions and normal Functions is: the normal Functions are defined using the def Keyword, and the anonymous Functions are defined using lambda. All anonymous functions in python are called lambda. The lambda syntax in python is lambda arguments: expression.
Characteristics of Python Lambda
- The python Lambda can have many arguments but carries only one Expression(a piece of code with or without a value)
- Lambda code can return Function objects
- Lamba code is only allowed one Expression.
Example of the lambda single Expression
Python Input
x = lambda a, b, c: a + b + c print(x(7, 9, 34))
Output: 50
Uses of the Python Lambda Function
The lambda function is mainly used when we want to operate on a nameless function within a short period. However, in Python, we generally use it as a function that takes in other functions as arguments (higher functions). Also, you can use lambda functions with other built-in functions like map, filter, and reduce.
Examples of Lambda Function with map
Python’s map() is a built-in function that enables you to operate and convert all the items in an iterable list without using a User-defined loop. Lambda with map will function like this: the function is created with all the items in the list and a new list is created which contains items returned by that function for each item.
Python Input
# Program to double each item in a list using a map() my_list = [1, 5, 4, 6, 8, 11, 3, 12] new_list = list(map(lambda x: x * 2 , my_list)) print(new_list)
Output: [2, 10, 8, 12, 16, 22, 6, 24]
Explanation of lambda returns
- The map takes in a function and a list
- The function is created with all the items in the list and a new list is created which contains items returned by that function for each item.
- The lambda returns double the value of each character.
Example of Lambda Function with filter
Python Input
# Program to filter out only the even items from a list my_list = [1, 5, 4, 6, 8, 11, 3, 12] new_list = list(filter(lambda x: (x%2 == 0) , my_list)) print(new_list)
Output: [4, 6, 8, 12]
The return filters out the even numbers only.
Explanation of the returns
- The map takes in a function and a list of arguments
- The function is called with all the items in the list and a new list is formed which contains items that the Function proves to be True.
Lambda Function with Reduce
The repeat() function is used to perform a repetition on the pairs of elements in the list. To use the reduce, you need to import reduce Functions from the function tools library.
Example
Python Input
# Using lambda inside reduce from functools import reduce list1 = [3,4,5,8,11,9,12,4,9] sum = reduce((lambda x,y: x+y), list1) print(sum)
Output: 65
Notice how the reduce() function iteratively multiplies through the elements in the list .i.e 3+4, 3+4+5,3+4+5+8, and progressively on.
We can create a lambda Function inside a lambda Function also known as the nested lambda
Python Nested Lambda
From the explanation above we know that nested means creating a Function inside another Function. In the example below we’ll be creating a nested lambda
Example:
Python Input
# Python program to demonstrate # nested lambda functions f = lambda a = 4, b = 6:lambda c: a+b+c o = f() print(o(4))
Output: 14
Lambda returns explanation
- The nested lambda function substitutes the value of a and b from the first as a=4 and b=6 respectively
- The value of c is realized from its caller o which passes c = 4.
- Then we get the returns which is the summation of a, b, and c which is 14.
Another example is shown below
Python Input
# Python program to demonstrate # nested lambda functions square = lambda x: x**4 product = lambda f, n: lambda x: f(x)*n and = product(square, 7)(9) print(ans)
Output: 45927
Summary
Python Lambda is an anonymous function in Python that performs like regular python Functions. They’re anonymous because they can be defined without a name.
A striking difference between the Python Lambda Functions and normal Functions is: that the normal Functions are defined using the def Keyword, and the anonymous Functions are defined using lambda. All anonymous functions in python are called lambda
The lambda function is mainly used when we want a nameless function on short notice. However, in Python, we generally use it as a function that takes in other functions as arguments (higher functions). Also, you can use lambda functions with other built-in functions like map, filter, and reduce.
You’ve learned the following:
- What is Python Lambda?
- Characteristics of lambda
- Uses of the lambda
- Lambda with map
- Lambda with filter
- Lambda with reduce
- Operations on lambda
- Python Nested Lambda
After this page, you shouldn’t be skeptical about taking the lambda adventure. For a head start see python Tutorials. Good luck Coding!