Alternatives for returning multiple values from a Python function
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Python functions always return one object. When people say a function "returns multiple values," what usually happens is that the function returns one container object and the caller unpacks it. The right container depends on how strongly you want to name the fields, how stable the shape is, and whether the return value should behave like data or like a richer domain object.
The default choice: return a tuple
For a small fixed set of values, a tuple is still the normal answer. Python even lets you write the return without parentheses, but the result is a tuple.
This is compact and familiar. It works best when the meaning of each position is obvious or when the caller immediately unpacks the result.
Use NamedTuple or a dataclass when names matter
If the return value has structure that deserves field names, a plain tuple starts to become hard to read. NamedTuple or a dataclass makes the result self-documenting.
This style is usually better when the result is passed around beyond the immediate call site, because field names carry meaning that positional unpacking does not.
Return a dictionary when the shape is flexible
A dictionary is useful when the set of returned fields may grow or vary, or when the caller naturally works with keyed data.
The tradeoff is that dictionaries are less strict. A misspelled key fails at runtime, and the expected schema is easier to drift over time unless you document it carefully.
Return a custom object when behavior belongs with the data
Sometimes the returned data is not just a bundle of values. It has methods, invariants, or behavior that belong together. In that case, returning a real class instance is often the cleanest design.
This is more than "multiple return values." It is a signal that the result has domain behavior and should be modeled explicitly.
A generator is not the same thing
Generators created with yield are often mentioned in lists like this, but they solve a different problem. A generator streams a sequence of values over time. It is not the normal replacement for returning a fixed bundle of related results from one function call.
If your function naturally produces a series, use yield. If it produces one structured answer with several parts, return a tuple, dataclass, dict, or object instead.
Common Pitfalls
The most common mistake is returning a plain tuple from a function whose positions are not obvious. That makes the call site brittle because readers have to remember what index 0 or 1 means.
Another issue is using a dictionary when the return shape is actually fixed and important. In that case, a dataclass or NamedTuple gives stronger structure and clearer intent.
Developers also treat generators as if they were "multiple returns." They are not. They represent sequential output, not a fixed structured result.
Finally, avoid returning many loosely related values just because Python makes it easy. If the result has a real concept in the domain, create a type for it.
Summary
- Python returns one object, even when the caller sees several unpacked values.
- Tuples are the standard answer for small fixed return groups.
- Dataclasses and
NamedTupleare better when field names matter. - Dictionaries are useful when the returned shape is flexible or schema-like.
- Generators solve streaming output, not structured multi-field return values.
Related reading
- Alternatives for returning multiple values from a Python function
- Amazon S3 boto - how to delete folder?
- amazon ses smtp python usage
- Amazon SES SMTP with Django
- An example using python bindings for SVM library, LIBSVM
- Anaconda export Environment file
- Anaconda ImportError /usr/lib64/libstdc.so.6 version GLIBCXX_3.4.21' not found
- Anaconda Integration with Cuda 9.0 shows Incompatible Package Error
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.