Python Lambda Function List Comprehension | Map, Filter, Reduce

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 those.

Lambda is one of the very useful and advanced topics from Python.

This tutorial is all about Python Lambda Function List Comprehension. I will also demonstrate the use of lambda function with map(), filter() and reduce().

At the end of this tutorial, you will understand every basic practical detail you should know to use the Lambda function in your Python code.

If you are preparing for a Job interview, there is a high probability you will find many Python interview questions from the Lambda function.

Let’s begin.

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 the Lambda function.

It uses keyword lambda. Here is the simple syntax for the lambda function

lambda : expression

Below is a simple example.

lambda x:x*2

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 the 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 a 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 a 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 a function name as well.

(lambda x:x*2)(10)

We can say, it is just like an inline function.
It will return output the same as 20.

Lambda Function with Single Arguments:

Write a program to double a number using the 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 the above program, we have assigned a 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 the lambda function in Python.

# Simple Program that shows the 
# use of lambda functions 
# with single argument

add = lambda x, y: x + y

print(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, 6]

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, 6]

Observe above both list comprehension and lambda/map() program carefully. You can see, there is the only difference between the rearrangement of words and the removal of ‘in’ and ‘for.’

Now you have seen, normal function and lambda function work the same. Then…

What is the major Use of the Lambda Function?

Lambda function is useful when you need passing function object to other functional concepts such as filter(), map() and reduce(). These 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 reads 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 a 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]

Python Lambda function comes very handily to automate certain tasks with a single line of code. With the single Lambda function call I can clear the Python interpreter console. It’s one of the very useful tricks I used every time while working with Python.

Summary

Here is a quick summary. Keep it in mind.

  • Lambda function is a very powerful and quick concept in Python programming.
  • In general, we use Python lambda function when we need a nameless function for a 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 the following articles to learn more about 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.

12 Comments

  1. Lambda function is one of the toughest topics in Python as I know. But, you have explained it in very simple manners, Sir. Thanks!

  2. This is one of a series of great explanations. Good job.
    There is one minor update you should make though:
    The output of the first list comprehension example should be [2, 4, 8, 10, 6].

Leave a Reply

Your email address will not be published. Required fields are marked *