Tuples in Practice
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Tuples show up all through everyday Python: a function returns several values, zip pairs two lists, and enumerate numbers items. Each of them uses small tuples.
Returning several values
Packing means putting several values into one tuple, as in return a, b or point = 3, 4. Unpacking is the reverse step, which splits the tuple into names again.
A function returns one object. To give back a smallest and a largest value together, return them as a tuple and unpack the result at the call.
def min_max(values): return min(values), max(values)
low, high = min_max([14, 3, 27, 9])print(low, high)result = min_max([5, 8])print(result)Example 1 · Quotient and remainder together
quotient, remainder = divmod(17, 5)print(quotient)print(remainder)print(divmod(17, 5))3 2 (3, 2)
What this code does
divmodis a built-in that returns a tuple.- Unpacking gives each part its own name.
- Printing the call shows the plain tuple.
return a, b packs the two values into one tuple. The caller can keep the tuple whole, as result, or unpack it straight into two names.
Pairing lists with zip
Zip is a built-in that joins two or more sequences position by position into tuples. It stops when the shortest sequence runs out.
Two lists often belong together, such as names and marks. zip walks them side by side and gives one tuple for each position.
names = ["Asha", "Ravi", "Meena"]marks = [82, 67, 91]for name, mark in zip(names, marks): print(name, mark)pairs = list(zip(names, marks))print(pairs)Example 2 · Total of price times quantity
prices = [120, 80, 45]quantities = [2, 5, 10]total = 0for price, qty in zip(prices, quantities): total += price * qtyprint(total)1090
What this code does
zippairs each price with its quantity.- The loop unpacks every pair into
priceandqty. - 1202 + 805 + 45*10 gives the total.
Each step of the loop gets a tuple such as ("Asha", 82), and the loop unpacks it. If the lists have different lengths, zip stops at the shortest one.
Numbering items with enumerate
To print a number beside each item you do not need an index loop. enumerate gives a tuple of position and item.
tickets = ["login bug", "slow page", "typo"]for number, title in enumerate(tickets, start=1): print(number, title)start=1 makes the count begin at 1 instead of 0. Without it, the first pair is (0, "login bug").
Comparing and sorting tuples
Tuples compare item by item from the left, like names in a phone book. That makes a list of tuples easy to sort and to search for its largest.
students = [("Meena", 91), ("Asha", 82), ("Ravi", 82)]print(sorted(students))print(max(students))print((2, 9) < (3, 1))print((2, 9) < (2, 10))Example 3 · Best score first
names = ["Asha", "Ravi", "Meena"]marks = [82, 67, 91]ranked = sorted(zip(marks, names), reverse=True)print(ranked)print(ranked[0][1])[(91, 'Meena'), (82, 'Asha'), (67, 'Ravi')] Meena
What this code does
- Putting the mark first in each tuple makes
sortedorder by mark. reverse=Trueputs the highest mark first.ranked[0][1]is the name in the first tuple.
(2, 9) < (3, 1) is decided by the first items, 2 and 3. Only when the first items are equal does Python look at the second. sorted puts "Asha" before "Meena" because of this.
Next step
Check your understanding
10 quick questions