Tuples and Sets
Use tuples for fixed data and sets for unique value operations
Tuples and sets are less used than lists, but they solve specific problems elegantly. Tuples are immutable lists — perfect for fixed data like column name mappings, coordinates, or database field definitions that should never change. Sets store only unique values — perfect for finding duplicates, unique categories, and set operations like intersection and difference.
Examples
Key Points
- ✓Tuple = immutable list (parentheses). Set = unique values (curly braces)
- ✓Tuple unpacking: a, b, c = (1, 2, 3) assigns all at once
- ✓set() on a list removes all duplicates in one step
- ✓Set intersection (&), union (|), difference (-) are powerful for data comparison
- ✓In Pandas: df["City"].unique() returns array of unique values like a set
Practice Question
You have a list with 1000 city names, many repeated. What is the fastest way to get only unique city names?