Python Lambda Function List Comprehension | Map, Filter, Reduce
In an earlier post, we have seen, 5 simplest programming languages for beginners. For obvious, Python is one of that. This post is all about Python Lambda Function List Comprehension. I will also demonstrate the use of lambda function with map(), filter() and reduce().
In this article, you can find every basic practical detail you should know to use Lambda function in your Python code.
What is Python Lambda Function?
In Python, the function which does not have a name or does not associate with any function name is called Lambda function.
It uses keyword lambda. Here is the simple syntax for the lambda function
lambda : expression
Below is a simple example.
Here ‘x’ is an argument and ‘x*2’ is an expression in a lambda function.
How are Lambda Functions Different from Normal Function Conceptually?
We already know about Python function. Normal function in python is created using keyword def as written below.
def multiply(x): x= x*2 return x
Differences and similarities between Lambda and Normal Functions:
- Lambda function can have multiple or any number of arguments as like normal function.
- It should have only one expression.
- It uses the implicit return statement. So many people call it as single expression function. The single expression is evaluated and returned.
Calling Lambda Function:
So, How is it possible to call this Python lambda function without its name?
You can assign lambda function to some function object. After that, you can make a call to lambda function as like normal function in python.
Here is a simple program.
multiply = lambda x:x*2 print multiply(10) #output: 20
In another way, you can make it work without function name as well.
We can say, it is just like an inline function.
It will return output same as 20.
Lambda Function with Single Arguments:
Write a program to double a number using lambda function in Python.
# Simple Program that shows the # use of lambda functions # with single argument twice = lambda x: x * 2 print(twice(5)) # Output: 10
In above program, we have assign lambda function object to multiply and then we are calling it as twice(10). It will give output as 20.
Lambda Function with Multiple Arguments.
Write a program to add two numbers using lambda function in Python.
# Simple Program that shows the # use of lambda functions # with single argument add = lambda x, y: x + y add(5, 3) #Output: 8
Python Lambda Function List Comprehension:
Looking at the given example for Python Lambda Function list comprehension. It multiplies each element of the list by two.
numbers = [1,2,4,5,3] double = [x*2 for x in numbers] print double #Output: [2, 4, 8, 10, 9]
You can do the same with python lambda function with map().
numbers = [1,2,4,5,3] double = map(lambda x: x*2 , numbers) print double #Output: [2, 4, 8, 10, 9]
Observe above both list comprehension and lambda/map() program carefully. You can see, there is the only difference of rearrangement of words and removal of ‘in’ and ‘for.’
Now you have seen, normal function and lambda function work the same. Then…
What is the major Use of Lambda Function?
Lambda function is useful when you need passing function object to other functional concepts such as filter(), map() and reduce(). These all three functional concepts have in common. They take two arguments: function object and list.
Take an example of the map.
Map Example using Lambda Function:
Write a program that read the sentence and gives the output as the length of each word in a sentence in the form of a list.
sentence = 'I am learning Python programming with CSEstack' words = sentence.split() print words ['I', 'learn', 'Python', 'programming', 'with', 'CSEstack'] lengths = map(lambda word: len(word), words) print lengths [1, 5, 6, 11, 4, 8]
Filter Example using Lambda Function:
Write a Python program that takes list containing numbers and returns all the odd numbers in the form of a list.
# Simple Program that filters out # only the odd items from # a list using filter() and # lambda functions my_list = [1, 9, 2, 7, 17, 44, 343, 14] new_list = list(filter(lambda x: (x%2 == 1) , my_list)) print(new_list) # Output: [1, 9, 7, 17, 343]
- Lambda function is very powerful and quick concept in Python programming.
- In general, we use Python lambda function when we need a nameless function for short duration.
- Mostly it is used to pass as a function object to other functional concepts such as map(), filter() and reduce().
You can also read following articles to learn more about Python:
- Difference between Mutable and Immutable in Python
- Python How to Randomly Select Item from List in Python
- Python How to Find Unique Elements from List in Python?
Many geeks find lambda function difficult, so I tried to keep it as simple as possible. I have explained Python lambda function list comprehension with map, filter, reduce examples. It makes understanding easy.If you have any doubt, write in a comment. I will keep posting more such tutorials. Stay tuned to learn and grow.