Why is lambda asking for 2 arguments despite being given 2 arguments?
Why do you use 2 arguments? filter()
and map()
require a function with a single argument only, e.g.:
filter(lambda x: x >= 2, [1, 2, 3])
>>> [2, 3]
To find the factors of a number (you can substitute it with lambda as well):
def factors(x):
return [n for n in range(1, x + 1) if x % n == 0]
factors(20)
>>> [1, 2, 4, 5, 10, 20]
If you run map or filter on a key-value set, then add parentheses around (k,v), like:
.filter(lambda (k,v): k*2 + v)
Because filter
in python takes only one argument. So you need to define a lambda/function that takes only one argument if you want to use it in filter.