Sometimes, we want to expand tuples into arguments with Python.
In this article, we’ll look at how to expand tuples into arguments with Python.
How to expand tuples into arguments with Python?
To expand tuples into arguments with Python, we can use the *
operator.
For instance, we write:
def add(a, b, c):
return a + b + c
res = add(*(1, 2, 3))
print(res)
to unpack the tuple (1, 2, 3)
with *
as the arguments of add
.
Therefore, a
is 1, b
is 2, and c
is 3.
And res
is 6.
Conclusion
To expand tuples into arguments with Python, we can use the *
operator.