Advanced

Returning more than one thing

A function returns one value, but that value can be a pair or a list. Returning several results at once is how a function reports more than one answer.

What you will see

(2, 9)

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Return two results from one call.

1

Return the smallest and largest of a list.

def min_max(items): return min(items), max(items) low, high = min_max([4, 9, 2]) print(low, high)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
2 9
2

Return a total and a count.

def stats(items): return [sum(items), len(items)] total, count = stats([1, 2, 3]) print(total, count)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
6 3
3

Return a found flag and a position.

def find(items, want): if want in items: return True, items.index(want) return False, -1 print(find([1, 2], 5))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
False -1
4

Unpack a pair into one name and see what happens.

pair = min_max([4, 9, 2]) print(pair)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
(2, 9)
One name holds the whole tuple. Unpacking needs one name per part.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A function returns one value, and that value can be a tuple or list holding several results that the caller unpacks.
1 min_max called
↓
2 both answers computed
↓
3 one tuple returned
↓
4 caller unpacks two names
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

def min_max(items): sets nothing on screen
return min(items), max(items) sets nothing on screen
low, high = min_max([4, 9, 2]) sets nothing on screen
print(low, high) 2 9
Why this works
The shape must match

When a function can return two different shapes, the caller has to guess. Keeping every return the same shape is what makes unpacking safe.

Named results

If the parts have names, returning a dict and reading by key beats unpacking by position. Position is fine for two parts, names are better for three or more.

Examples

Return a pair

def min_max(items):
    return min(items), max(items)

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

The comma makes one tuple holding two values. The call unpacks it back into two names.

Return a list

def stats(items):
    return [sum(items), len(items)]

total, count = stats([1, 2, 3])
print(total, count)

A list works the same way. The caller takes the parts it needs by position.

Return nothing useful yet

def find(items, want):
    if want in items:
        return True, items.index(want)
    return False, -1

print(find([1, 2], 5))

Both paths return the same shape. A function that sometimes returns one value and sometimes two is the bug this avoids.

One value is often enough

def first(items):
    return items[0]

print(first([7, 8]))

Returning one value stays simplest. Reach for a pair only when the caller really needs two answers.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Where a name can be seen
Previous Lesson
Where a name can be seen
Next Lesson
Splitting a program into functions
Splitting a program into functions →