What are Multiple Return Values Actually in Python? (28/100 Days of Python) (2024)

What are Multiple Return Values Actually in Python? (28/100 Days of Python) (2)

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

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

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 are Multiple Return Values Actually in Python? (28/100 Days of Python) (2024)
Top Articles
What are the best ways to summarize focus group findings?
Tradingview | Saxo Markets
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6052

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.