How to unpack multiple tuples in function call

99封情书 提交于 2019-12-03 22:25:17

As of the release of Python 3.5.0, PEP 448 "Additional Unpacking Generalizations" makes the natural syntax for this valid Python:

>>> f(*tup1, *tup2)
1 2 2 3

In older versions of Python, you can need to concatenate the tuples together to provide a single expanded argument:

>>> tup1 = 1, 2
>>> tup2 = 2, 3
>>> def f(a, b, c, d):
        print(a, b, c, d)

>>> f(*tup1+tup2)
1 2 2 3

Another approach using chain

>>> from itertools import chain
>>> def foo(a,b,c,d):
        print a,b,c,d


>>> tup1 = (1,2)
>>> tup2 = (3,4)
>>> foo(*chain(tup1,tup2))
1 2 3 4
Spacedman
f(tup1[0],tup1[1],tup2[0],tup2[1]) #not good enough?
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!