What are Multiple Return Values Actually in Python? (28/100 Days of Python)
In Python, it is possible to return multiple values from a function. This can be done by separating the values with a comma. The returned values can then be assigned to multiple variables. This can be very useful in many scenarios. But what do the multiple return values actually represent? How are they implemented under the hood by the language itself?
Python allows us to return several values from a function. Thus making it possible to write code like the following:
def get_user_data():
return 'Anna', 23, 'anna123'
name, age, id = get_user_data()
print('Got the user data:', name, age, id)
# Got the user data: Anna 23 anna123
Yet, in reality, the language actually allows only a single return value. Let’s see how this magic happens.
Let’s get the value returned by the function and print its type and its value:
def get_user_data():
return 'Anna', 23, 'anna123'
res = get_user_data() # Store the returned value in a single variable
print(res) # ('Anna', 23, 'anna123')
print(type(res)) # <class 'tuple'>
name, age, id = res # Unpack the tuple
print('Got the user data:', name, age, id)
# Got the user data: Anna 23 anna123
So, it’s a tuple! Multiple returned values from a function are actually packed into a tuple and then returned from the function as a single variable. If we “inline” this function it would look like this:
res = 'Anna', 23, 'anna123' # Which is an alternative of ('Anna', 23, 'anna123')
print(res) # ('Anna', 23, 'anna123')
print(type(res)) # <class 'tuple'>
# Unpack the tuple:
name, age, id = res
print('Got the user data:', name, age, id)
# Got the user data: Anna 23 anna123
# Which is exactly the same as:
name, age, id = 'Anna', 23, 'anna123'
print('Got the user data:', name, age, id)
# Got the user data: Anna 23 anna123
Other ways to return multiple values
There are other ways that allow returning multiple values from a function. We can return a list containing the needed items, we can return a dictionary that maps the keys to the needed elements, or any other data structure that allows us to pack several values into one and return it:
def get_numbers_list():
return [3, 4]
[a, b] = get_numbers_list()
print(a) # 3
print(b) # 4
def get_numbers_dict():
return {'a':3, 'b':4}
numbers = get_numbers_dict()
print(numbers['a']) # 3
print(numbers['b']) # 4
What’s the beauty of tuples?
Tuples allow one to pack and unpack several values without any additional syntax like brackets. The values are separated with commas making them seem like separate values.
# Return a list
def get_numbers_list():
return [3, 4]
[a, b] = get_numbers_list()
print(a) # 3
print(b) # 4
# Return multiple values
def get_numbers_list():
return 3, 4
a, b = get_numbers_list()
print(a) # 3
print(b) # 4
# Return a tuple
def get_numbers_list():
return (3, 4)
a, b = get_numbers_list()
print(a) # 3
print(b) # 4
# Wrap a and b in a tuple
def get_numbers_list():
return 3, 4
(a, b) = get_numbers_list()
print(a) # 3
print(b) # 4
All of the examples above will result in exactly the same values for a
and b
. Yet, returning comma-separated values and using those returned values as comma-separated values feels more natural when operating with those.
What’s next?
- If you found this story valuable, please consider clapping multiple times (this really helps a lot!)
- Hands-on Practice: Free Python Course — functions that have multiple return values
- Full series: 100 Days of Python
- Previous topic: Functions
- Next topic: 4 Types of Function Arguments in Python that You Might Not Know About