Beginner·6 min·basics · tuples · sets
Tuples & Sets
Tuples & Sets
Tuples
(1, 2, 3)— like a list, but immutable (can't change after creation)- Useful for fixed records:
point = (x, y),(name, age) - Unpacking:
a, b = (1, 2)is one of Python's loveliest features
Sets
{1, 2, 3}— unordered collection of unique values- O(1) membership check (much faster than a list for "is x in this?")
- Set algebra:
|union,&intersect,-difference
Try it
- Deduplicate a list with
list(set(items)). - Find words that appear in both
"the quick brown fox"and"the lazy dog".