Overuse of lambda expressions in Python (original) (raw)

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 :

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)

`

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

`

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

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:

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)

`

5

Python3 `

Python program performing

operation using lambda

d = (lambda x, y, z: x*y+z)(1, 2, 3) print(d)

`

5

Cons on lambda functions:

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)

`

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)

`

three

Misuse of Lambda expressions :

func = lambda x, y, z: x*y + z

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

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

`

nums = [-2, -1, 0, 1, 2] sort = sorted(nums, key=lambda x: abs(x))

sort = sorted(nums, key=abs)

nums = [1, 2, 3, 4, 5] summation = reduce(lambda x, y: x + y, nums)

nums = [1, 2, 3, 4, 5] summation = sum(nums)

Overuse of lambda expressions :

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)

`

[{'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}]

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)

`

[{'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}]

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

def Key(person): name, sex, age = person return sex sorted_people = sorted(people, key=Key)

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

`

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

`

nums = [1, 2, 3, 4, 5] product = reduce(lambda x, y: x*y, nums, 1)

nums = [1, 2, 3, 4, 5] def multiply(nums): prod = 1 for number in nums: prod *= number return prod product = multiply(nums)