Blue Monday Blue Monday - 27 days ago 18
Python Question

Understanding how Lambda function works

Help me understand Lambda functions, I checked a lot of codes but I still can't manage to understand it. Maybe with a simple example, thanks in advance.


Suppose you want to square in value in a list , foo = [1,2,3,4,5]

for i in range(len(a)):
    a[i] = a[i] * a[i] 

You could use lambda and write

map(lambda x: x * x, foo)

if you want only elements divisible by 3, then

filter(lambda x: x % 3 == 0, foo)

Basically it save you from writing a for loop or to put it better write it in an efficient way.