Tuples and sets

Introduction

A tuple is like a list that cannot change, written with round brackets. A set is an unordered collection that keeps each value only once. Both are everyday tools for grouping values.

Lists are general purpose, but sometimes you want something more specific. A tuple says "these values belong together and will not change", while a set answers "is this in the group?" far faster than a list can.

How it works

Tuples

point = (3, 4)
x, y = point
distance = (x * x + y * y) ** 0.5
print(point, distance)
Open in compiler

point = (3, 4) groups two values. x, y = point unpacks the tuple into two variables in one line. Line 3 shows the calculation (x * x + y * y) ** 0.5 → (3 * 3 + 4 * 4) ** 0.5 → 5.0, the distance from (0, 0).

Tuples cannot be changed: point[0] = 5 is a TypeError. Use a tuple for values that belong together and should stay fixed, such as a coordinate or a date.

Sets

marks = [72, 85, 72, 90, 85]
unique = set(marks)
unique.add(60)
print(len(marks), len(unique))
print(85 in unique)
Open in compiler

set(marks) keeps each value once, so the five marks become three unique values (72, 85 and 90); after add(60) there are four. In the objects panel the set has no position numbers, because sets have no order. 85 in unique checks membership, and it is fast even for very large sets.

Combining sets

cse = {1, 2, 3, 4}
eee = {3, 4, 5}
print(cse & eee)
print(cse | eee)
print(cse - eee)
Open in compiler
OperatorMeaningResult here
a & bin both{3, 4}
a | bin either{1, 2, 3, 4, 5}
a - bin a but not in b{1, 2}

Syntax

point = (3, 4)
x, y = point

unique = set([1, 1, 2])
2 in unique
a & b      # in both
a | b      # in either
a - b      # in a only

Example

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

low, high = min_max([7, 2, 9, 4])
print(low, high)
Open in compiler

return min(nums), max(nums) returns one tuple holding two values. Step to the return: the frame shows the returned tuple, and line 4 unpacks it into low and high.

Line by line

  1. def min_max(nums): defines the function.
  2. return min(nums), max(nums) makes one tuple holding both values.
  3. The call on the fourth line gets that tuple, and unpacks it into low and high.
  4. print shows the two values.

Expected result

2 9

In the real world

A coordinate, a date and a row from a database fit naturally into tuples. Sets remove duplicate entries from a mailing list and check quickly whether a username is taken.

Common mistakes

  • (5) is just the number 5. A one-item tuple needs a comma: (5,).
  • {} is an empty dictionary, not a set. Write set() for an empty set.
  • Expecting my_set[0] to work. Sets have no positions; loop over them or check membership with in.

Best practices

  • Return a tuple when a function produces a small, fixed group of results.
  • Convert a list to a set when you make many membership tests.
  • Use sorted when you need a set's values in order.

Summary

Tuples are fixed groups that unpack neatly, and a comma makes one. Sets keep one copy of each value, have no order, and support fast membership tests and set operations.

Practice

Read two lines of numbers and print the numbers that appear in both, in increasing order. Use set(), & and sorted().

Quick check

1. What is len(set([1, 1, 2, 2, 2]))?
2. After a, b = (7, 8), what is b?
3. Which line makes an empty set?

Key takeaways

  • Use a tuple for values that belong together and should stay fixed.
  • A set removes repeats, and answers in very quickly.
  • Empty sets are written set(), because {} is a dictionary.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.