Python list comprehension lambda

In this Python tutorial, we will discuss about Python list comprehension.

  • What is Python list comprehension?
  • What is lambda?
  • Lambda function using filter()
  • Lambda function using map()
  • Python list comprehension lambda closure
  • Python list comprehension vs lambda
  • Python list comprehension vs map

What is Python list comprehension?

List comprehension is used to create a new list based on the existing list. The list comprehension is used to work on the list. List comprehension returns the list and It contains expression and brackets.

Syntax for Python list comprehension

[expression for item in list if condition == True]

List comprehension

In this example, I have taken a list as chocolate and if condition is used. Here, the if condition is true. So it returned a new list.

Example:

chocolate = ["5star", "silk", "milkybar"]
newchocolate_list = [a for a in chocolate if "5" in a]
print(newchocolate_list)

You can refer the below screenshot for the output, we can see the new list.

Python list comprehension
Python list comprehension

Now, we will check what happens if the items are not present in the list. Then, it will return an empty list because the if condition is not true. You can refer to the below screenshot.

Python list comprehension
Python list comprehension

What is lambda in Python?

Lambda is a function that is defined without a name. Usually in python functions are defined with a keyword def but anonymous functions are defined by the keyword lambda. The lambda function is used along with the built-in function map(), filter().

In this example, we can see how to find the cube of a given number using lambda function.

Example:

cube = lambda a: a * a * a
print(cube(5))

In the below screenshot you can see the output:

What is lambda in Python
What is lambda in Python

Python Lambda function using filter()

  • In this example, I have taken a list of numbers and assigned the values from 1 to 12 in it.
  • The filter() function is called with all the items in the list and returns a new list.
  • And then we can use the lambda function using the filter() to get the multiples of 4 from the list.

Example:

number = [1,2,3,5,4,6,8,9,11,12]
multiples_of_4 = list(filter(lambda a: (a%4 == 0) ,number))
print(multiples_of_4)

Below screenshot shows the output, we can see the multiples of 4 from the list in the output:

Lambda function using filter()
Lambda function using filter()

Python Lambda function using map()

  • In this example, I have taken a name of list as numbers and assigned multiples of 5 and each number is divided by 5 using map().
  • The map() is called with all the items in the list and a new list is returned.

Example:

number = [5,10,15,20,25,30,35,40,45,50]
newnumber = list(map(lambda c: c/5 ,number))
print(newnumber)

You can refer below screenshot for the output:

Lambda function using map()
Lambda function using map()

Python list comprehension lambda closure

  • The closure is used to create resource globally and reuse it in a function to avoid performance issues.
  • Closure can be used only in nested functions. When we call the lambda function it will take the ‘_’ value from the closed namespace.
  • It will not take the ‘_’ value when the lambda object is created. ‘_’ value lives in the namespace.
  • In this example, I have used c = [_() for _ in b]. It works here because a new namespace is used for all the names except the original input.
  • In b = [lambda: _ for _ in a] here I have used different names for loop target.we are not masking over the closed over name.
  • ‘_’ value remains in the namescope and returns the value 4 for 4 times.

Example:

a = [1, 2, 3 , 4]
b = [lambda: _ for _ in a]
c = [_() for _ in b]
print(a)
print(b)
print(c)

Here, In this output we can see last element returns 4 times.

Python list comprehension lambda closure
Python list comprehension lambda closure

Python list comprehension vs lambda

Let us see the difference between Python list comprehension and lambda.

  • List comprehension is used to create a list.
  • Lambda function process is the same as other functions and returns the value of the list.
  • List comprehension is more human-readable than the lambda function.
  • User can easily understand where the list comprehension is used .
  • List comprehension performance is better than lambda because filter() in lambda is slower than list comprehension.

Python list comprehension vs map

Let us try to understand Python list comprehension vs map.

  • List comprehension allows filtering, there is no filtering in the map.
  • List comprehension returns the list of results whereas the map only returns the map objects.
  • The map is faster to call the defined function whereas lambda is not required to call the defined function.
  • Map performance is better comparing to list comprehension.

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python list comprehension lambda. Also, We covered these below topics:

  • What is Python list comprehension?
  • What is lambda?
  • Lambda function using filter()
  • Lambda function using map()
  • Python list comprehension lambda closure
  • Python list comprehension vs lambda
  • Python list comprehension vs map