How to Pass a Tuple as an Argument to a Function?
In this article, we will learn how to pass a tuple of elements as an argument to a function in Python. We will use some custom code to better understand the topic.. Let's first have a quick look over what is a tuple in Python.
Python has a built-in data type called a tuple. Data inside a tuple can be of any type say, integer, string or a float value, or even a tuple type. The tuple uses comma-separated values within round brackets or parentheses to store data. Tuples can be defined using any variable name and then assigning different values to the tuple inside the round brackets. The tuple is ordered, unchangeable, and allows duplicate values.
tuple1 = ("Ram", "Arun", "Kiran") tuple2 = (16, 78, 32, 67) tuple3 = ("apple", "mango", 16, "cherry", 3.4)
Pass a Tuple as an Argument to a Function
In Python, a tuple is defined with multiple elements separated by a comma within parentheses or round brackets. Parentheses differentiate a tuple from other data types like list, dictionary etc. But, Python language standards state that parentheses are not mandatory for a tuple.
Below three cases are equivalent and signify a tuple in python. For user convenience, Python constructs a temporary tuple as needed for an assignment statement. Thus, all three of the assignment statements are exactly the same once they reach data movement.
a, b = 1, 2 a, b = (1, 2) (a, b) = 1, 2
Example: Pass a Tuple as an Argument using *args Syntax
This method of passing a tuple as an argument to a function involves unpacking method. Unpacking in Python uses
*args syntax. As functions can take an arbitrary number of arguments, we use the unpacking operator
* to unpack the single argument into multiple arguments. This is a special way of receiving parameters to a function as a tuple. Due to the * prefix on the args variable, all extra arguments passed to the function are stored in args as a tuple.
def powersum(power, *args): #Return the sum of each argument raised to the specified power. total = 0 for i in args: total += pow(i, power) return total #function call print(powersum(2, 3, 4))
Example: Pass a Tuple as an Argument as an Individual Variable
A tuple can also be passed as a single argument to the function. Individual tuples as arguments are just individual variables. A function call is not an assignment statement; it's a reference mapping. Therefore, the semantics are different. In the below example, the first function call explicitly defines the tuple using a variable and then passes the variable as an argument. Another function call passes the tuple enclosed within round brackets instead of explicitly defining it.
def func(myTuple): first, second = myTuple return first #Driver Code myTuple = (2,3) #function call 1 print(func(myTuple)) #function call 2 print(func((2, 3)))
In this article, we used two examples to understand how can we pass a tuple as an argument to a function. We discussed
*args syntax in Python to unpack the arguments of the tuple and use them separately in the function body and another method involves explicitly defining the tuple and then passing it as an argument.