Categories
Python Answers

How to expand tuples into arguments with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *