Overuse of lambda expressions in Python

What are lambda expressions?
A lambda expression is a special syntax to create functions without names. These functions are called lambda functions. These lambda functions can have any number of arguments but only one expression along with an implicit return statement. Lambda expressions return function objects. For Example consider the lambda expression:

lambda (arguments) : (expression)

This lambda expression defines an unnamed function, which accepts two arguments and returns the sum of the two arguments. But how do we call an unnamed function? The above defined unnamed lambda function can be called as:

(lambda x, y: x + y)(1, 2)

Code 1:

filter_none

editclose

play_arrow

linkbrightness_4code

# Python program showing a use

# lambda function

# performing a addition of three number

x1 =(lambdax, y, z: (x +y) *z)(1, 2, 3)

print(x1)

# function using a lambda fumction

x2 =(lambdax, y, z: (x +y) if(z ==0) else(x *y))(1, 2, 3)

print(x2)

chevron_right

filter_none

Output:

9
2

Though it is not encouraged, the function object returned by a lambda expression can be assigned to a variable. See the example below in which a variable sum is assigned a function object returned by a lambda expression.

filter_none

editclose

play_arrow

linkbrightness_4code

# Python program showing

# variable is storing lambda

# expression

# assingned to a variable

sum=lambdax, y: x +y

print(type(sum))

x1 =sum(4, 7)

print(x1)

chevron_right

filter_none

Output:

11

Common uses of lambda expressions :

Since lambda functions are anonymous and do not require a name to be assigned, they are usually used to call functions(or classes) which require a function object as an argument. Defining separate functions for such function arguments is of no use because, the function definition is usually short and they are required only once or twice in the code. For example, the key argument of the inbuilt function, sorted().

filter_none

editclose

play_arrow

linkbrightness_4code

# Python program showing

# using of normal function

defKey(x):

returnx%2

nums =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

sort =sorted(nums, key =Key)

print(sort)

chevron_right

filter_none

Output:

[0, 2, 4, 6, 8, 1, 3, 5, 7, 9]

filter_none

editclose

play_arrow

linkbrightness_4code

# Python program showing use

# of lambda function

nums =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

sort_lambda =sorted(nums, key =lambdax: x%2)

print(sort_lambda)

chevron_right

filter_none

Output:

[0, 2, 4, 6, 8, 1, 3, 5, 7, 9]

Lambda functions are inline functions and hence they are used whenever there is a need of repetitive function calls to reduce execution time. Some of the examples of such scenarios are the functions: map(), filter() and sorted(). For example,

filter_none

editclose

play_arrow

linkbrightness_4code

# Python program showing a use

# of lambda function

nums =[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

# using map() function

squares =map(lambdax: x *x, nums)

print(list(squares))

# using filter() function

evens =filter(lambdax: Trueif(x %2==0)

elseFalse, nums)

print(list(evens))

chevron_right

filter_none

Pros and Cons of lambda functions :Pros of lambda functions:

Being anonymous, lambda functions can be easily passed without being assigned to a variable.

Though both codes do the same thing, the second one which uses def is much more readable. The expression written here under the lambda might be simple, but it has a meaning(formula for compound interest). Hence, the expression is non-trivial and deserves a name. Using lambda expressions for non-trivial functions reduces the readability of the code.

Using lambdas when multiple lines would help :If using a multiple line function makes the code more readable, using lambda expressions to reduce some lines of code is not worth it. For example, see the code below.

See how tuple unpacking in the second block of code makes it much more readable and logical. Readibility of the code should the be utmost priority of a programmer, working in a collaborative environment.

Using lambda expressions for map and filter : Lambdas are very commonly used with map() and filter() as shown.

Unlike map() and filter(), generator expressions are general purpose features of python language. Thus generators enhance the readability of the code. While, map() and filter() require prior knowledge of these functions.

Use of higher order functions : The functions which accept other function objects as arguments are called higher order functions (i.e. map() and filter()), which are common in functional programming. As stated above, lambda expressions are commonly used as the function arguments of higher order functions. Compare the two code blocks shown below.
Using high order function reduce()

While the first block uses fewer lines of code and is not that difficult to understand, programmers with no background of functional programming will find the second block of code much readable. Unless practicing proper functional programming paradigm, passing one function to another function is not appreciated, as it can reduce readability.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.