+ 3

lambda functions vs normal functions in python

What are the situation(s) that lambda functions are an advantage to normal functions in python?

21st Aug 2017, 4:43 PM
S. Saeed Hosseini
S. Saeed Hosseini - avatar
1 ответ
+ 2
The main advantage (as I see it) is that lambda is a one-line function that can be used on the fly. For example, in list comprehensions, maps, filters: numbers = list(range(20)) [*filter(lambda x:x %2==0,numbers)] # take only pair numbers or as a key in certain functions: a = [2, 4, 9, 7] indices = sorted(range(len(a)),key=lambda x:a[x]) # sort a If you had to use a normal function using "def" if would be cumbersome and less effective.
21st Aug 2017, 6:25 PM
BossaNova
BossaNova - avatar