Skip to main content

How to Return Two Values from a Function in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A Python function can hand back two results at once: write them after return with a comma between them, and catch them in two names when you call it. Behind the scenes, Python packs them into a tuple. This guide returns the lowest and highest number of a list, on Python 3.15.2.

Syntax​

Syntax
def <function>(<parameters>):
return <value 1>, <value 2>


<name 1>, <name 2> = <function>(<arguments>)
PartWhat it means
return <value 1>, <value 2>Returns both values, packed into one tuple
<name 1>, <name 2> = ...Unpacks the tuple: the first value goes to the first name, the second to the second

The Python tutorial calls this tuple packing, "the values 12345, 54321 and 'hello!' are packed together in a tuple", and its reverse "sequence unpacking".

The Example​

lowest_and_highest takes a list of numbers and returns its smallest and largest. The script catches them in two names, then prints the function's result as it is.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

min_max.py
def lowest_and_highest(numbers):
return min(numbers), max(numbers)


low, high = lowest_and_highest([7, 2, 9, 4])
print(low, high)
print(lowest_and_highest([7, 2, 9, 4]))

The min_max.py script in a macOS-style code window: lowest_and_highest returns min(numbers), max(numbers); the result is unpacked into low and high and printed, then printed as it is.

Step 2: Run It​

Terminal
uv run python min_max.py
Output
2 9
(2, 9)

The terminal after uv run python min_max.py: 2 9, then (2, 9).

low got 2 and high got 9. Printed without unpacking, the result shows what the function really returns: the tuple (2, 9). The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Unpacking into the wrong number of names: low, middle, high = lowest_and_highest(...)ValueError: not enough values to unpack (expected 3, got 2)Use as many names as the function returns
Writing two return linesThe function stops at the first one, so the second never runsPut both values in one return, separated by a comma

Troubleshooting​

SymptomLikely causeFix
not enough values to unpack or too many values to unpackThe number of names doesn't match the number of valuesCount the values after return

FAQ​

How do I write the type hint?​

As a tuple of the two types: def lowest_and_highest(numbers: list[int]) -> tuple[int, int]:.

Can I keep the result in one name?​

Yes. result = lowest_and_highest([7, 2, 9, 4]) keeps the tuple, and print(result[0], result[1]) printed 2 9.

Conclusion​

return min(numbers), max(numbers) returned two values packed into the tuple (2, 9), and low, high = ... unpacked them into two names. Match the number of names to the number of values.

Additional Resources​