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
Current Balance vs. Available Balance - SmartAsset
Data Reveals The Industries with the Easiest Interviews
Moon Stone Pokemon Heart Gold
Top 10: Die besten italienischen Restaurants in Wien - Falstaff
Routing Number 041203824
Parks in Wien gesperrt
Campaign Homecoming Queen Posters
Best Restaurants Ventnor
Washington Poe en Tilly Bradshaw 1 - Brandoffer, M.W. Craven | 9789024594917 | Boeken | bol
People Portal Loma Linda
Vermont Craigs List
Kiddle Encyclopedia
Unity - Manual: Scene view navigation
Publix Super Market At Rainbow Square Shopping Center Dunnellon Photos
ELT Concourse Delta: preparing for Module Two
If you bought Canned or Pouched Tuna between June 1, 2011 and July 1, 2015, you may qualify to get cash from class action settlements totaling $152.2 million
Garnish For Shrimp Taco Nyt
Exl8000 Generator Battery
Www Craigslist Madison Wi
Atlases, Cartography, Asia (Collection Dr. Dupuis), Arch…
Elbert County Swap Shop
Shoe Station Store Locator
Meridian Owners Forum
Fiona Shaw on Ireland: ‘It is one of the most successful countries in the world. It wasn’t when I left it’
Dtm Urban Dictionary
2023 Ford Bronco Raptor for sale - Dallas, TX - craigslist
Cylinder Head Bolt Torque Values
Jailfunds Send Message
Ts Modesto
1964 Impala For Sale Craigslist
Mkvcinemas Movies Free Download
15 Downer Way, Crosswicks, NJ 08515 - MLS NJBL2072416 - Coldwell Banker
Metra Union Pacific West Schedule
Midsouthshooters Supply
Restored Republic June 6 2023
Barstool Sports Gif
Bunkr Public Albums
Dinar Detectives Cracking the Code of the Iraqi Dinar Market
Vindy.com Obituaries
Former Employees
Centimeters to Feet conversion: cm to ft calculator
Hello – Cornerstone Chapel
Wild Fork Foods Login
Poster & 1600 Autocollants créatifs | Activité facile et ludique | Poppik Stickers
Diamond Desires Nyc
Wvu Workday
M Life Insider
28 Mm Zwart Spaanplaat Gemelamineerd (U999 ST9 Matte | RAL9005) Op Maat | Zagen Op Mm + ABS Kantenband
Duffield Regional Jail Mugshots 2023
Unity Webgl Extreme Race
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.