To return multiple values from a Python function, use a comma to separate the return values.
For instance, let’s create a function that takes two numbers as arguments. This function returns the sum, difference, multiplication, and division between these two numbers.
Here is how it looks in the code:
def operate(a, b): sum = a + b diff = a - b mul = a * b div = a / b return sum, diff, mul, div
Now you can call this function for two numbers and assign the return values to variables:
n1 = 5 n2 = 10 sum, diff, mul, div = operate(n1, n2) print( f"The sum is {sum}\n" f"The difference is {diff}\n" f"The multiplication gives {mul}\n" f"The division gives {div}\n" )
This results in the following being printed in the console:
The sum is 15 The difference is -5 The multiplication gives 50 The division gives 0.5
How Does Returning Multiple Values Work in Python
In the previous section, you learned how to return multiple values by comma-separating the values.
But why and how does it work?
Tuples in Python
The example code works because it returns a tuple.
In case you don’t know what a tuple is, check out this in-depth article. In short, a tuple is a group of zero or more elements.
For instance, here is an example of a tuple of three values that represents a 3D point:
coords = (1, 1, 3)
Notice that Python tuples do not always need parenthesis.
For example, you can write the above 3D point as:
coords = 1, 1, 3
This creates the same tuple of three values that represent a 3D point.
How to Access a Tuple Value in Python
To access and store a value from a tuple you can access it like you would access a value from a list. In other words, use the [] operator with an index.
For example, let’s store the 3D points into variables x, y, and z:
coords = 1, 1, 3 x = coords[0] y = coords[1] z = coords[2] print(x, y, z)
This results in the following output in the console:
1 1 3
This works fine, but there is an easier way for this particular purpose.
Tuple Destructuring in Python
You can use tuple destructuring to access tuple values and store them into variables.
Tuple destructuring means you declare a bunch of comma-separated variables in one line and assign each tuple value to the corresponding variable. This saves you lines of code meanwhile it also makes the intention very clear.
Let me show what I mean
For instance, in the previous example, you stored 3D coordinates x, y, and z on separate lines using the [] operator. Instead of doing it this way, you can utilize tuple destructuring as a shorthand:
coords = 1, 1, 3 x, y, z = coords print(x, y, z)
As a result, each coordinate in the 3D point is assigned to a separate variable.
1 1 3
As you can see, the first value of the tuple was attached to variable x, the second one to y, and the third one to z.
Now you understand how tuples are created and how values are read from them.
How to Return Multiple Values from a Function
To return multiple values from a function, return the values as a tuple. To then access/store these values into variables, use tuple destructuring.
If you now look at the example you saw in the introduction:
def operate(a, b): sum = a + b diff = a - b mul = a * b div = a / b return sum, diff, mul, div
You recognize this function returns a tuple of four values. To access these four values, you use tuple restructuring. Here’s how it looks in code:
n1 = 5 n2 = 10 sum, diff, mul, div = operate(n1, n2) print( f"The sum is {sum}\n" f"The difference is {diff}\n" f"The multiplication gives {mul}\n" f"The division gives {div}\n" )
Output:
The sum is 15 The difference is -5 The multiplication gives 50 The division gives 0.5
Now the returned tuple values are stored in the variables sum, diff, mul, and div.
Destructuring a returned tuple this way is handy. You don’t need to use the square-bracket accessing operator to manually pick the values from the tuple. Instead, the destructuring syntax takes care of that.
Conclusion
To return multiple values from a function in Python, return a tuple of values.
As you may know, a tuple is a group of comma-separated values. You can create a tuple with or without parenthesis. To access/store the multiple values returned by a function, use tuple destructuring.
Thanks for reading. Happy coding!