Python
programming
return values
multiple return
coding tips

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.

python
1def parse_name(full_name: str):
2    first, last = full_name.split()
3    return first, last
4
5
6first_name, last_name = parse_name("Ada Lovelace")
7print(first_name)
8print(last_name)

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.

python
1def stats(numbers: list[int]):
2    total = sum(numbers)
3    count = len(numbers)
4    average = total / count
5    return total, count, average
6
7
8_, _, avg = stats([10, 20, 30])
9print(avg)

You can ignore values at the beginning, middle, or end of the tuple.

python
total, _, average = stats([5, 15, 25])
print(total, average)

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.

python
1def head_middle_tail(items: list[int]):
2    return items
3
4
5first, *_, last = head_middle_tail([1, 2, 3, 4, 5])
6print(first)
7print(last)

Here *_ captures the unused middle values. You can also keep the middle if needed:

python
first, *middle, last = [1, 2, 3, 4, 5]
print(middle)

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.

python
1def divide(a: float, b: float):
2    if b == 0:
3        return False, None
4    return True, a / b
5
6
7ok, result = divide(12, 3)
8print(ok, result)
9
10_, result_only = divide(20, 5)
11print(result_only)

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.

python
1from dataclasses import dataclass
2
3
4@dataclass
5class Stats:
6    total: int
7    count: int
8    average: float
9
10
11def stats_object(numbers: list[int]) -> Stats:
12    total = sum(numbers)
13    count = len(numbers)
14    return Stats(total=total, count=count, average=total / count)
15
16
17result = stats_object([10, 20, 30])
18print(result.average)

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.

python
value, _ = (10, 99)
print(_)  # 99

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.

python
1def get_user_record():
2    return ("alice", (101, "admin"), True)
3
4
5username, (_, role), _ = get_user_record()
6print(username)
7print(role)

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.

python
1def dimensions():
2    return 1920, 1080
3
4
5size = dimensions()
6print(size[0])

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.

Course illustration
Course illustration

All Rights Reserved.