Can I specify other arguments in map ?
For example, I have the following code:
def f(a, b):
return a + b
l = [1, 2, 3]
ll = map(f, l)
How can I give an argument to the map above so that each element of ll is the sum of one element in l and the given argument?
For example, if I can use something like map(f(2,), l), I will get [3, 4, 5] as result.
I know I can achieve the same result by list comprehension, or a for loop, but I just want to know if it is possible to do it in a map way.
mapitself does not directly provide a way to do that. However, you can do it by usingfunctools.partialto pre-specify the static argument:However, as @jamylak suggested in a comment, there’s little reason to do this. If you don’t want to store the result, just do a regular for loop: