Python#2 · 3 Dec 2026
Adding a list to a set
seen = set()
seen.add((1, 2))
seen.add((1, 2))
print(len(seen))
seen.add([3, 4])What does this print?
Python · References and mutability
seen = set()
seen.add((1, 2))
seen.add((1, 2))
print(len(seen))
seen.add([3, 4])What does this print?
It prints: 1
TypeError
A set stores each value once, so the second (1, 2) is a duplicate and the size stays 1. Set members and dict keys must be hashable, which in practice means immutable. A tuple of numbers can never change, but a list can, so seen.add([3, 4]) raises TypeError: unhashable type: 'list'.
Mutable objects such as lists, dicts and sets cannot be set members or dict keys; use a tuple instead.