Tuples
Data structures4 min readLesson 1 of 3
A tuple is an ordered, immutable sequence. Once built, it cannot change.
python
point = (3, 5)
x, y = point # unpacking
point[0] # 3Because it never changes, a tuple is hashable and can be a dictionary key or a set member. A list cannot.
python
seen = {(0, 0), (1, 2)}
locations = {"start": (0, 0), "end": (4, 7)}Use a tuple for a fixed record (a point, an RGB colour, a row of coordinates) and a list for a collection that grows. Python also lets you return several values at once, which is just a tuple:
