← Back to Articles
Tutorial

Return Multiple Values in Python

Learn how to return multiple values in Python with examples.

Python is a versatile programming language that allows you to return multiple values from a function. This feature can simplify your code and enhance its readability.

In Python, you can return multiple values using tuples, lists, or dictionaries. For instance, returning a tuple can be done like this: def func(): return 1, 2. Another method is using a dictionary for more descriptive keys.

For best practices, choose the return type based on the context: tuples for fixed collections, lists for variable-sized collections, and dictionaries for key-value pairs.

A common mistake is assuming that functions can only return one value, leading to unnecessarily complex code. Also, ensure you're unpacking returned values correctly.

Code Examples

Example 1

def get_coordinates(): return 10, 20
x, y = get_coordinates()

Example 2

def get_user_info(): return {'name': 'Alice', 'age': 30}
user_info = get_user_info()

More Python Tutorials