Pitchkrak
Pitchkrak

Reputation: 370

Using map python with key argument

I have a function that looks like this:

def myFunct(arg1=None,arg2=None,arg3=None):
    pass

I would like to use that function with a map function but with argument 1 and 3 only. idea would be:

map(myFunct,list_arg1,list_arg3)

so each of the call would be myFunct(value1,arg3=value3)

How could I achieve that ?

Upvotes: 0

Views: 166

Answers (2)

John Coleman
John Coleman

Reputation: 52008

Another approach is to keep your function as is and modify what you are mapping over:

from itertools import repeat
def f(x = 0, y = 0, z = 0):
    return sum((x,y,z))

map(f,range(1,10),repeat(0),range(21,30))

Although from a readability point of view, a simple generator expression might be preferable to any solution based on map, something along the lines of:

f(x = i,z = j) for i,j in zip(range(1,10),range(21,30)))

Upvotes: 0

Axe319
Axe319

Reputation: 4365

You could use lambda to map the arguments to your keyword arguments.

def foo(arg1=None, arg2=None, arg3=None):
    return arg1 + arg3

list1 = [3, 4, 5]
list2 = [5, 6, 7]

print(list(map(lambda x, y: foo(arg1=x, arg3=y), list1, list2)))

Upvotes: 5

Related Questions