6

I have a multi-variable function and i would like to use the map() function with it.

Example:

def f1(a, b, c):
    return a+b+c
map(f1, [[1,2,3],[4,5,6],[7,8,9]])

3 Answers 3

10

itertools.starmap made for this:

import itertools

def func1(a, b, c):
    return a+b+c

print list(itertools.starmap(func1, [[1,2,3],[4,5,6],[7,8,9]]))

Output:

[6, 15, 24]
Sign up to request clarification or add additional context in comments.

Comments

5

You can't. Use a wrapper.

def func1(a, b, c):
    return a+b+c

map((lambda x: func1(*x)), [[1,2,3],[4,5,6],[7,8,9]])

Comments

3

You can simply wrap your multi-argument function inside another function that takes just one argument as a tuple/list and then passes it on to the inner function.

map(lambda x: func(*x), [[1,2,3],[4,5,6],[7,8,9]])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.