Creating Tuples
Concepts covered: Tuples
A tuple is an ordered sequence, like a list, that cannot change after Python builds it. You write one as values separated by commas, usually inside parentheses. Reach for a tuple when a record has a fixed shape: a GPS point is always a latitude and a longitude, and a 5K run always has 5 kilometre splits. Once the record exists, no later line of code can overwrite one of its values by accident.
point = (38.7223, -9.1393)
splits = (312, 298, 305, 291, 284)
run = ("r_4821", "Lisbon", 5, splits)
empty = ()Reading from a tuple works the way list reading does: index 0 is the first item, -1 is the last, and an item inside a nested tuple takes a second index. The run record holds 4 items of 3 types: 2 strings, an integer and a tuple.
point = (38.7223, -9.1393)
splits = (312, 298, 305, 291, 284)
run = ("r_4821", "Lisbon", 5, splits)
print(point[0])
print(splits[-1])
print(run[1])
print(run[3][0])
print(type(run) == tuple)Output:
38.7223 284 Lisbon 312 True
The comma builds the tuple
The comma is what tells Python to build a tuple. Parentheses only group, as they do in (2 + 3) * 4. That has 2 consequences: values separated by commas with no parentheses around them are already a tuple, and a single value wrapped in parentheses stays that value. A tuple with 1 item needs a trailing comma.
coords = 38.7223, -9.1393
in_parens = (5)
one_item = (5,)
print(coords)
print(type(coords) == tuple)
print(type(in_parens) == tuple)
print(type(one_item) == tuple)
print(one_item)Output:
(38.7223, -9.1393) True False True (5,)
Python prints a one-item tuple with its trailing comma, (5,), so the printed form shows which kind you are holding.
Which line creates a tuple holding exactly 1 item, the number 7?
- item = (7)
- item = (7,)
- item = [7]
- item = ()
A tuple refuses every change
A list lets you replace, append and remove items. A tuple has no method that changes it, and assigning to one of its indexes stops the program with a TypeError at the line that attempted the edit.
splits = (312, 298, 305, 291, 284)
print("before:", splits)
splits[0] = 300
print("after:", splits)The first print runs, then line 3 raises TypeError: 'tuple' object does not support item assignment, and after: never prints. That final line of the error names the problem. Calling splits.append(280) also stops the program, with AttributeError: 'tuple' object has no attribute 'append', because tuples have no append method.
To correct a value, build a new tuple and assign it to the name, as in point = (38.7223, -9.1400). That line points the name point at a brand-new tuple, and the original was never edited. This is the safety a tuple buys: a record's values change only where code writes out a whole new record on purpose.
- list
- Square brackets: [312, 298]
- Items can be replaced, appended and removed
- Suits data that grows, such as today's uploads
- Refused as a dictionary key or set member
- tuple
- Commas, usually in parentheses: (312, 298)
- Fixed at creation; an edit raises an error
- Suits a record of known shape, such as a GPS point
- Accepted as a key or set member when its contents are
Tuples as dictionary keys
The Sets lesson showed that a set refuses a list as a member because a list can change. Dictionary keys follow the same rule, and a tuple of strings and numbers passes it. That makes a tuple the right key when no single value is unique: a runner logs many dates and a date holds many runners, but the pair (runner, date) picks out a single day's distance.
daily_km = {
("ana", "2026-03-14"): 5,
("ana", "2026-03-15"): 8,
("ben", "2026-03-14"): 12,
}
print(daily_km[("ana", "2026-03-15")])
daily_km[("ben", "2026-03-15")] = 6
print(daily_km[("ben", "2026-03-15")])
print(("ana", "2026-03-16") in daily_km)Output:
8 6 False
About this lesson
This section is part of the Collections: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.