0

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 ?

1

2 Answers 2

5

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)))
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect thanks. possible edit : test1 and test2 on the last line are iterables. So maybe name them list1, list2.
No problem. I changed it.
0

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)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.