Kodeclik Blog

How to return a tuple in Python

Let us suppose you wish to write a Python function that returns the square of a number like so:
The output will be:
You can also write a function to compute cubes like:
which will yield:
Now what if we desired a function that computed both squares and cubes and returned them both? How can we do that? A Python function can only return one object, not two. The solution is to package the square and cube into a single tuple and return the tuple.
In Python, tuples are an immutable data structure that allows you to store multiple values in a single variable. While they are often used for grouping related data, tuples also offer the flexibility to return multiple values from a function.
A tuple is defined by enclosing comma-separated values within parentheses, like this: (value1, value2, value3). Tuples can contain elements of different data types, and their values can be accessed using indexing or slicing, just like lists.
Thus, we can update our program as:
In this program, in the function powers(x), we first compute the square and cube in variables “square” and “cube”. Then we package them into a tuple (note the extra brackets). In the main program, the result of powers(5) is received in another tuple (s,c) and we print both of these values on separate lines. The output is:
In fact, we need not be this careful in our return statement. The following code works equally well:
Here, we are simply listing the variables in the return statement. By default the return statement packages its arguments into a tuple.
In other words, it is the comma(s) that make the tuple not quite the parentheses. But if you are returning an empty tuple (this is not true in our example), you do need parentheses.
You can also remove the parentheses in the line where you unpack your tuple. For instance:
works correctly. Note the lack of parentheses in the line where we unpack the results into variables “s” and “c”. The output will be:
Finally, you have to be careful in unpacking your tuple after returning from your function. For instance, if you did:
You will get the following error:
This is because the powers() function returns a tuple of two values whereas the statement in the main program seems to expect a tuple of three values.
In summary, returning tuples in Python is a powerful technique that allows you to bundle and extract multiple values from a function. Tuples offer a lightweight and efficient way to group related data, making code more readable and maintainable.
Returning a tuple in Python is very easy as we have seen here. Just package them and return it or simply list all components of your tuple in the return statement. Likewise in unpacking them, use an assignment expression with the correct number of tuple arguments. By mastering the art of returning tuples, you can enhance the flexibility and expressiveness of your Python programs.
If you liked this blogpost, learn how to convert a list to a tuple in Python!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2023. All rights reserved.