Pass non-iterable variables to a function evaluated with map()
For one thing, if you map your function over a range
, no parameter is an iterable.
To your question, you can bind positional parameters (from left to right) to a function using functools.partial
def func(g,h,i):
return i*(g+h)
print map(functools.partial(func, 2,3), range(20))
# [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
For binding any positional parameter, use a lambda expression like stated in hkpeprah's answer.
If you know the parameters ahead of time, you can use a lambda like
f = lambda lst: func(2,3,lst)
map(f, range(20))
Alternatively, if you don't know the parameters, you could wrap a lambda expression
f = lambda x: lambda y: lambda lst: func(x,y,lst)
f = f(2)
f = f(3)
map(f, range(20))