Course Content
Python Essentials for AI Engineer
6 sections · 48 lessons
What is the difference between List and Tuple?
What you need to know
Syntax and the one-item trap
1batch = ["doc1", "doc2"] # list: square brackets2point = (12.97, 77.59) # tuple: parentheses (Bengaluru lat, lon)3single = ("doc1",) # a one-item tuple NEEDS the comma4not_a_tuple = ("doc1") # just a str in brackets5print(type(single).__name__, type(not_a_tuple).__name__) # tuple strIt is the comma that makes a tuple, not the brackets. x = 1, 2 is also a tuple.
Mutability
1batch.append("doc3") # fine2print(batch) # ['doc1', 'doc2', 'doc3']3try:4 point[0] = 13.05except TypeError as e:6 print(e) # 'tuple' object does not support item assignmentImmutable means the tuple's slots cannot be reassigned. It is shallow: if a slot holds a list, that list can still change. t = ([1], 2); t[0].append(9) works, and such a tuple is no longer hashable.
Hashable: why tuples can be keys
A hashable object has a hash value that never changes during its life, which is what a dict or set needs to find it again. Immutable objects qualify; lists do not.
| List | Tuple | |
|---|---|---|
| Syntax | [1, 2, 3] | (1, 2, 3) |
| Mutable | Yes | No |
| Dict key / set member | No | Yes, if its items are hashable |
| Methods | many (append, sort, ...) | two (count, index) |
| Memory | a little more (spare room for growth) | a little less |
| Use for | a changing collection of similar items | a fixed record of related values |
Unpacking
Tuples shine when a function returns several values:
1def evaluate(preds, labels):2 correct = sum(p == l for p, l in zip(preds, labels))3 return correct, correct / len(labels) # returns a tuple45count, accuracy = evaluate([1, 0, 1, 1], [1, 0, 0, 1])6print(count, accuracy) # 3 0.75A real-life example
Embedding calls cost money, so a RAG service caches them. The cache key must identify both the model and the text, and it must be hashable:
1cache = {}2key = ("embed-small-v3", "What is my refund status?")3cache[key] = [0.12, -0.05, 0.33] # list VALUE is fine4print(key in cache) # True56try:7 cache[["embed-small-v3", "hi"]] = [0.1]8except TypeError as e:9 print(e) # unhashable type: 'list'10 # (3.14 words it as "cannot use 'list' as a dict key ...")The vector itself is a list (in real code, a NumPy array) because it is data you may process; the key is a tuple because it is a fixed identity. If 30% of support questions repeat each day, this one dictionary cuts the embedding bill by roughly the same share.
Follow-up questions to expect
- "Can a tuple ever change?" — Its slots can't be reassigned, but a mutable object inside it (like a list) can be modified in place.
- "When would you use a namedtuple or a dataclass instead?" — When the fields deserve names:
point.latreads better thanpoint[0]. Use@dataclass(frozen=True)for an immutable record with type hints. - "Is a tuple faster than a list?" — Slightly faster to create and a bit smaller, but choose based on meaning (fixed record or changing collection), not speed.