Sets and Tuples
Mar 28, 2026
In This Chapter
- What tuples are and when to use them over lists
- How to pack and unpack tuples
- What sets are and how they enforce uniqueness
- Set operations: union, intersection, difference
- The performance characteristics of sets vs lists
Tuples
A tuple is an immutable , ordered sequence. Once created, its contents can't change.
point = (3, 4)
rgb = (255, 128, 0)
single = (42,) # trailing comma required for single-element tuple
empty = ()You can also create tuples without parentheses — Python infers them from the commas:
coords = 3, 4 # same as (3, 4)Access elements the same way as lists (index, slice):
point[0] # 3
point[-1] # 4
point[0:2] # (3, 4)When to Use Tuples vs Lists
Use a tuple when the collection is fixed and its position has meaning:
# Good uses of tuples
rgb = (255, 128, 0) # fixed 3-component color
coordinates = (lat, lon) # positional meaning
return x, y # returning multiple valuesUse a list when the collection is mutable or homogeneous:
scores = [88, 92, 75] # may change
names = ["Alice", "Bob"] # may grow/shrinkTuples are also slightly faster in many cases and can be used as dictionary keys when all of their contents are hashable (lists can't).
Unpacking
Unpacking assigns tuple elements to variables in one line:
x, y = (3, 4)
name, age, city = ("Alan", 25, "Boston")
# swap two variables without a temp
a, b = 1, 2
a, b = b, aUse * to capture the rest:
first, *rest = (1, 2, 3, 4, 5)
# first = 1, rest = [2, 3, 4, 5]
*head, last = (1, 2, 3, 4, 5)
# head = [1, 2, 3, 4], last = 5Sets
A set is an unordered collection of unique elements. Duplicates are automatically removed.
tags = {"python", "backend", "python"}
print(tags) # {"python", "backend"} — duplicate removedCreate an empty set with set() (not {} — that's an empty dict):
empty = set()Common operations:
s = {1, 2, 3}
s.add(4) # {1, 2, 3, 4}
s.discard(2) # {1, 3, 4} — no error if missing
s.remove(3) # {1, 4} — KeyError if missing
3 in s # False — O(1) lookupSet Operations
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a | b # union: {1, 2, 3, 4, 5, 6}
a & b # intersection: {3, 4}
a - b # difference: {1, 2}
a ^ b # symmetric diff: {1, 2, 5, 6}These also work as methods: a.union(b), a.intersection(b), etc.
Sets for Deduplication and Fast Lookup
# deduplicate a list (order not preserved)
unique = list(set([1, 2, 2, 3, 3, 3]))
# [1, 2, 3]
# O(1) membership test vs O(n) for list
valid_ids = {101, 102, 103}
if user_id in valid_ids: # fast
...Key Questions
Q: What is the difference between a list and a tuple?
Lists are mutable — you can add, remove, or change elements. Tuples are immutable — once created, they can't be modified. Use tuples for fixed data where position has meaning (e.g., coordinates, RGB values, function return values). Tuples can be used as dictionary keys; lists cannot. Tuples are also marginally faster to iterate and create.
Q: Why can't you use a list as a dictionary key?
Dictionary keys must be hashable — they need a stable hash value that doesn't change over time. Lists are mutable, so their contents (and thus their hash) could change, breaking the dictionary's internal structure. Tuples are immutable and hashable, so they can be used as keys.
Q: How does a set enforce uniqueness?
A set is implemented as a hash table. When you add an element, Python computes its hash and checks if that hash already exists. If so, the element is not added. This means all elements in a set must be hashable, and membership testing is O(1) regardless of set size.
Q: What is the time complexity of
infor a list vs a set?
For a list, x in list is O(n) — it scans every element until it finds a match. For a set, x in set is O(1) on average because sets use a hash table for direct lookup. If you're doing many membership checks, convert the list to a set first.
Q: What is the difference between
discard()andremove()on a set?
Both remove an element. remove() raises a KeyError if the element isn't in the set. discard() does nothing if the element is missing. Use discard() when the element may or may not be present; use remove() when its absence would be a bug.