How to Return Multiple Values from Function in Python
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 tuplereturn [value1, value2, value3]— returns a listreturn {'key1': value1, 'key2': value2}— returns a dictionary
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.
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}')
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.
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)
Quick Reference
Use this quick guide to choose how to return multiple values:
| Method | Description | Example Return |
|---|---|---|
| Tuple | Default way, simple and fast | return 1, 2, 3 |
| List | When order matters and you want a mutable collection | return [1, 2, 3] |
| Dictionary | When you want named values for clarity | return {'name': 'Alice', 'age': 30} |