0
0
PythonHow-ToBeginner · 3 min read

How to Return Multiple Values from Function in Python

In Python, you can return multiple values from a function by separating them with commas, which creates a tuple. You can also return a list or dictionary to group multiple values together and return them as one object.
📐

Syntax

To return multiple values, list them separated by commas after the return keyword. Python automatically packs these values into a tuple. You can also return a list or dictionary if you want named or ordered collections.

  • return value1, value2, value3 — returns a tuple
  • return [value1, value2, value3] — returns a list
  • return {'key1': value1, 'key2': value2} — returns a dictionary
python
def example():
    return 1, 2, 3  # returns a tuple

def example_list():
    return [1, 2, 3]  # returns a list

def example_dict():
    return {'a': 1, 'b': 2}  # returns a dictionary
💻

Example

This example shows a function returning multiple values as a tuple, which are then unpacked into separate variables.

python
def get_user_info():
    name = 'Alice'
    age = 30
    city = 'New York'
    return name, age, city

user_name, user_age, user_city = get_user_info()
print(f'Name: {user_name}')
print(f'Age: {user_age}')
print(f'City: {user_city}')
Output
Name: Alice Age: 30 City: New York
⚠️

Common Pitfalls

One common mistake is forgetting that returning multiple values actually returns a tuple, so trying to access them like separate return statements won't work. Also, if you return a tuple but try to assign it to a single variable without unpacking, you get the whole tuple as one object.

Another pitfall is returning mutable objects like lists or dictionaries and then modifying them outside the function, which can cause unexpected changes.

python
def wrong_return():
    return 1
    return 2  # This line is never reached

result = wrong_return()
print(result)  # Output: 1

# Correct way:
def correct_return():
    return 1, 2

result = correct_return()
print(result)  # Output: (1, 2)
Output
1 (1, 2)
📊

Quick Reference

Use this quick guide to choose how to return multiple values:

MethodDescriptionExample Return
TupleDefault way, simple and fastreturn 1, 2, 3
ListWhen order matters and you want a mutable collectionreturn [1, 2, 3]
DictionaryWhen you want named values for clarityreturn {'name': 'Alice', 'age': 30}

Key Takeaways

Return multiple values by separating them with commas to create a tuple.
You can also return a list or dictionary to group multiple values.
Unpack returned tuples into variables for easy access.
Avoid unreachable code by using only one return statement per function call.
Be careful when returning mutable objects to prevent unintended side effects.