I have a list consisting of tuples, I want to pass each tuple\'s elements to a function as arguments:
mylist = [(a, b), (c, d), (e, f)]
myfunc(a, b)
myfunc(
To make @DSM's comment explicit:
>>> from itertools import starmap
>>> list(starmap(print, ((1,2), (3,4), (5,6))))
# 'list' is used here to force the generator to run out.
# You could instead just iterate like `for _ in starmap(...): pass`, etc.
1 2
3 4
5 6
[None, None, None] # the actual created list;
# `print` returns `None` after printing.
This is actually very simple to do in Python, simply loop over the list and use the splat operator (*
) to unpack the tuple as arguments for the function:
mylist = [(a, b), (c, d), (e, f)]
for args in mylist:
myfunc(*args)
E.g:
>>> numbers = [(1, 2), (3, 4), (5, 6)]
>>> for args in numbers:
... print(*args)
...
1 2
3 4
5 6