Ignore python multiple return value
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Python functions often return several related values at once, usually by returning a tuple. In many real code paths, you only need one or two of those values and want to ignore the rest without making the code messy. This article explains the standard unpacking patterns, when to use _, and how to keep multi-value returns readable.
Multiple Return Values Are Tuple Unpacking
When a Python function returns multiple values, Python packs them into a tuple and lets you unpack them into separate variables.
If you do not need every returned item, you can still unpack the tuple and deliberately discard parts of it.
Ignore Values with _
The most common convention is to assign unused values to _. This signals to readers that the value exists but is intentionally ignored.
You can ignore values at the beginning, middle, or end of the tuple.
This is the clearest choice when the function returns a fixed, known number of elements.
Use Star Unpacking for Variable-Length Results
If the returned tuple or list can vary in length, star unpacking is safer than naming many unused variables.
Here *_ captures the unused middle values. You can also keep the middle if needed:
This works well for parser functions or APIs that return a sequence where only boundary values matter.
Ignore Return Values from Built-Ins and Library Calls
This pattern appears frequently with helper functions that return status plus payload.
Ignoring values is fine as long as the discarded part is truly irrelevant in that context. If the first value communicates important success or error state, skipping it can be risky.
Prefer Clear APIs When Too Many Values Are Ignored
If callers repeatedly ignore most return values, the function signature may be doing too much. Consider returning a dataclass, named tuple, or dedicated object instead.
This can be easier to maintain than unpacking several positional values, especially as the function evolves.
The Difference Between _ as Convention and Special Meaning
In plain Python, _ is only a normal variable name by convention. It does not automatically disappear.
That means:
- using
_multiple times is fine for ignored values - the last assigned
_is still available in scope - in interactive shells,
_may already have special meaning for the previous result
If that could confuse readers, use names like _unused_count instead.
Nested Unpacking Also Works
You can ignore values inside nested structures returned by a function.
Nested unpacking is powerful, but use it carefully. If the structure is not obvious, this style becomes hard to scan quickly.
When Simple Indexing Is Better
If you only need one element once, unpacking is not always the clearest choice.
This is acceptable for short-lived values, but named unpacking is usually more readable for business logic.
Common Pitfalls
- Ignoring a status or error flag that should be checked before using the payload.
- Using
_so many times that the unpacking line becomes difficult to understand. - Relying on positional return order when a named object would be clearer.
- Forgetting that
_is still a real variable and can be overwritten. - Using unpacking patterns that break if the function later changes its return shape.
Summary
- Python multiple returns are usually tuples, which you can unpack directly.
- Use
_to ignore fixed-position values you do not need. - Use star unpacking when the number of unused values can vary.
- Consider returning a dataclass or named object if callers ignore many fields.
- Do not ignore values that communicate success, failure, or important state.

