Open In App

Overuse of lambda expressions in Python

Last Updated : 04 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

Python3




# Python program showing a use
# lambda function
 
# performing a addition of three number
x1 = (lambda x, y, z: (x + y) * z)(1, 2, 3)
print(x1)
 
# function using a lambda function     
x2 = (lambda x, y, z: (x + y) if (z == 0) else (x * y))(1, 2, 3)
print(x2)      


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. 
 

Python3




# Python program showing
# variable is storing lambda
# expression
 
# assigned to a variable
sum = lambda x, y: x + y
print(type(sum))
 
x1 = sum(4, 7)
print(x1)


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()
     

Python3




# Python program showing
# using of normal function
def Key(x):
    return x%2
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sort = sorted(nums, key = Key)
print(sort)


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

Python3




# Python program showing use
# of lambda function
 
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sort_lambda = sorted(nums, key = lambda x: x%2)
print(sort_lambda)


  • 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, 
     

Python3




# Python program showing a use
# of lambda function
 
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
 
# using map() function
squares = map(lambda x: x * x, nums)
print(list(squares))
 
# using filter() function
evens = filter(lambda x: True if (x % 2 == 0)
                              else False, nums)
print(list(evens))


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

  • Being anonymous, lambda functions can be easily passed without being assigned to a variable.
  • Lambda functions are inline functions and thus execute comparatively faster.
  • Many times lambda functions make code much more readable by avoiding the logical jumps caused by function calls. For example, read the following blocks of code. 
     

Python3




# Python program performing
# operation using def()
def fun(x, y, z):
    return x*y+z
a = 1
b = 2
c = 3
 
# logical jump
d = fun(a, b, c)
print(d)    


  • Output:
5

Python3




# Python program performing
# operation using lambda
 
d = (lambda x, y, z: x*y+z)(1, 2, 3)
print(d)


  • Output:
5

Cons on lambda functions: 

  • Lambda functions can have only one expression.
  • Lambda functions cannot have a docstring.
  • Many times lambda functions make code difficult to read. For example, see the blocks of code given below. 
     

Python3




def func(x):
    if x == 1:
        return "one"
    else if x == 2:
        return "two"
    else if x == 3:
        return "three"
    else:
        return "ten"
num = func(3)
print(num)


  • Output:
three

Python3




# Python program showing use
# of lambda function
num = (lambda x: "one" if x == 1 else( "two" if x == 2
                       else ("three" if x == 3 else "ten")))(3)
print(num)


  • Output:
three

  
Misuse of Lambda expressions : 

  • Assignment of lambda expressions : The official python style guide PEP8, strongly discourages the assignment of lambda expressions as shown in the example below.
func = lambda x, y, z: x*y + z
  • Instead, it is recommended to write a one-liner function as,
def func(x, y, z): return x*y + z 
  • While the second method encourages the fact that lambda functions are anonymous, it is also useful for tracebacks during debugging. Run the code below to see how def makes tracebacks much useful. 
     

Python3




func = lambda x, y, z: x * y + z
print(func)
def func(x, y, z): return x * y + z
print(func)


  • Wrapping lambda expressions around functions : Many times, lambda expressions are needlessly wrapped around functions as shown below.
nums = [-2, -1, 0, 1, 2]
sort = sorted(nums, key=lambda x: abs(x))
  • While the above syntax is absolutely correct, programmers must understand that all functions in python can be passed as function objects. Hence the same code can(and should) be written as,
sort = sorted(nums, key=abs)
  • Passing functions unnecessarily : Many times, programmers pass functions which perform only a single operation. See the following code.
nums = [1, 2, 3, 4, 5]
summation = reduce(lambda x, y: x + y, nums)
  • The lambda function passed above performs only a single operation, adding the two arguments. The same result can be obtained by the using the built-in function sum, as shown below. 
     
nums = [1, 2, 3, 4, 5]
summation = sum(nums)
  • Programmers should avoid using lambda expressions for common operations, because it is highly probable to have a built-in function providing the same results.

  
Overuse of lambda expressions : 

  • Using lambda for non-trivial functions :Sometimes, simple functions can be non-trivial. See the code below.

Python3




details = [{'p':100, 'r':0.01, 'n':2, 't':4},
           {'p':150, 'r':0.04, 'n':1, 't':5},
           {'p':120, 'r':0.05, 'n':5, 't':2}]
sorted_details = sorted(details,
                        key=lambda x: x['p']*((1 + x['r']/
                                x['n'])**(x['n']*x['t'])))
print(sorted_details)


  • Output:

[{‘n’: 2, ‘r’: 0.01, ‘t’: 4, ‘p’: 100}, {‘n’: 5, ‘r’: 0.05, ‘t’: 2, ‘p’: 120}, {‘n’: 1, ‘r’: 0.04, ‘t’: 5, ‘p’: 150}] 
 

  • Here, we are sorting the dictionaries on the basis of the compound interest. Now, see the code written below, which uses def.

Python3




details = [{'p':100, 'r':0.01, 'n':2, 't':4},
           {'p':150, 'r':0.04, 'n':1, 't':5},
           {'p':120, 'r':0.05, 'n':5, 't':2}]
def CI(det):
    '''sort key: compound interest, P(1 + r/n)^(nt)'''
    return det['p']*((1 + det['r']/det['n'])**(det['n']*det['t']))
sorted_details = sorted(details, key=CI)
print(sorted_details)


  • Output:

[{‘n’: 2, ‘r’: 0.01, ‘t’: 4, ‘p’: 100}, {‘n’: 5, ‘r’: 0.05, ‘t’: 2, ‘p’: 120}, {‘n’: 1, ‘r’: 0.04, ‘t’: 5, ‘p’: 150}] 
 

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

people = [(‘sam’, ‘M’, 18), (‘susan’, ‘F’, 22), (‘joy’, ‘M’, 21), (‘lucy’, ‘F’, 12)] 
sorted_people = sorted(people, key=lambda x: x[1]) 
 

  • Also see the following code which uses def.
def Key(person):
    name, sex, age = person
    return sex
sorted_people = sorted(people, key=Key)
  • See how tuple unpacking in the second block of code makes it much more readable and logical. Readability 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.

Python3




nums = [0, 1, 2, 3, 4, 5]
mapped = map(lambda x: x * x, nums)
filtered = filter(lambda x: x % 2, nums)
print(list(mapped))
print(list(filtered))


Python3




nums = [0, 1, 2, 3, 4, 5]
mapped = (x * x for x in nums)
filtered = (x for x in nums if x % 2 == 1)
print(list(mapped))
print(list(filtered))


  • 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()
nums = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x*y, nums, 1)
  • Without using high order function
nums = [1, 2, 3, 4, 5]
def multiply(nums):
    prod = 1
    for number in nums:
        prod *= number
    return prod
product = multiply(nums)
  • 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.


Previous Article
Next Article

Similar Reads

Python: Iterating With Python Lambda
In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python. Syntax: lambda variable : expression Where, variable is used in the expressionexpression can be an mathematical expressio
2 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Lambda and filter in Python Examples
Prerequisite : Lambda in Python Given a list of numbers, find all numbers divisible by 13. Input : my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70] Output : [65, 39, 221] We can use Lambda function inside the filter() built-in function to find all the numbers divisible by 13 in the list. In Python, anonymous function means that a function is witho
2 min read
Map function and Lambda expression in Python to replace characters
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul We have an existing solution for this problem in C++. Please refer to Replace a character c1 with c2 and c2 with c1 in a string S. We can solve th
2 min read
Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space. Examples: Input : [1, 2, 3, 2, 3, 1, 3] Output : 3 We have existing solution for this problem please refer Find the Number Occurring Odd Number of Times link. we will solv
1 min read
Intersection of two arrays in Python ( Lambda expression and filter function )
Given two arrays, find their intersection. Examples: Input: arr1[] = [1, 3, 4, 5, 7] arr2[] = [2, 3, 5, 6] Output: Intersection : [3, 5] We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function. Implementation: C/C++ Code # Fun
1 min read
Using lambda in GUI programs in Python
Python Lambda Functions are anonymous function means that the function is without a name. In this article, we will learn to use lambda functions in Tkinter GUI. We use lambda functions to write our code more efficiently and to use one function many times by passing different arguments. We use the lambda function to pass arguments to other functions
3 min read
Difference between List comprehension and Lambda in Python
List comprehension is an elegant way to define and create a list in Python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp. A list comprehension generally consists of these parts : Output expression,Input sequence,A variable representing a member of the input sequence
3 min read
Python | Find fibonacci series upto n using lambda
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ........ In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1. Find the series of fibonacci numbers using lambda function. Code #1 : B
2 min read
Python | sympy.Lambda() method
With the help of sympy.Lambda() method, we can perform any mathematical operation by just defining the formula and then pass the parameters with reference variable by using sympy.Lambda(). Syntax : sympy.Lambda() Return : Return the result of mathematical formula. Example #1 : In this example we can see that we are able to perform any mathematical
1 min read
Article Tags :
Practice Tags :