How can I return two values from a function in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Python functions do not literally return two separate objects; they return one object that can contain multiple pieces of data. In everyday code, that usually means returning a tuple and unpacking it at the call site. The real question is not whether Python can return two values, but which container makes the result easiest to read and maintain.
Return a Tuple for Simple Results
The most common solution is to return two values separated by commas. Python packs them into a tuple automatically.
Output:
This is concise and idiomatic. It works well when the meaning of the two results is obvious from the function name or from variable names used during unpacking.
Use Named Results When Meaning Matters
A bare tuple can become unclear when the values are similar or easy to swap accidentally. In those cases, use a namedtuple or dataclass so the caller can access fields by name.
This is slightly longer than a tuple, but it scales better once the return type becomes part of a public API.
Return a Dictionary When the Shape Is Flexible
If the result fields are optional or vary based on the operation, a dictionary can be a reasonable choice.
A dictionary is useful for loosely structured data, but it is easier to mistype keys and harder to refactor than a tuple or data class. Use it when flexibility matters more than strict structure.
Unpacking Is Part of the Design
Returning multiple values is only half of the pattern. The call site should unpack them clearly. Python lets you ignore values you do not need.
This makes function calls concise, but it also means order matters. If the function returns (status, data), the caller must know which position means what. Named return objects reduce that risk.
When to Raise Instead of Returning Two Values
Sometimes developers return a pair such as (result, error) because they want to report success and failure in one object. In Python, exceptions are often the cleaner choice for real error cases.
Return two values when both are normal outputs. If one of them represents an exceptional failure path, the code is usually clearer when the function raises.
Pick the Lightest Useful Structure
A practical rule is:
- Use a tuple for short, obvious return values.
- Use a
dataclassornamedtuplewhen names improve clarity. - Use a dictionary only when the result is intentionally flexible.
- Use exceptions instead of
(value, error)for actual failures.
That rule keeps Python code idiomatic without sacrificing readability.
Common Pitfalls
- Returning a plain tuple when the meaning of each position is easy to confuse later.
- Using a dictionary for fixed structured data that would be clearer as a tuple or
dataclass. - Forgetting the return order and unpacking the values into the wrong variables.
- Returning
(result, error)patterns from code that would be simpler with exceptions. - Changing a function from one return shape to another without updating all callers.
Summary
- Python commonly returns two values by packing them into a tuple.
- Tuple unpacking makes the call site concise and readable for simple cases.
- '
dataclassandnamedtupleare better when the result benefits from named fields.' - Dictionaries are useful for flexible response shapes but weaker for fixed APIs.
- Choose the return type based on how clearly callers can understand and use the result.

