Chapter 09

Sets

Unordered, unindexed, no duplicates. Sets are hash tables — elements must be hashable. Understand frozenset, set operations, and when sets beat lists.

Questions: 12
Difficulty: ★★☆☆☆
Creating sets: {1, 2, 3} is a set. But {} is an empty dict, NOT an empty set! To create an empty set: set(). This surprises many people.
Basics & Deduplication
Q 01Predict the Output
s = {1, 1, 2, 2, 3}
print(s)
print(len(s))
print(type({}))
Output
{1, 2, 3} 3 <class 'dict'>
Why? Sets automatically deduplicate. {1,1,2,2,3} becomes {1,2,3}. Important: {} creates an empty dict, not a set. To get an empty set: set().
Q 02Multiple Choice
s = {True, 1, 1.0, 2}
print(s)
print(len(s))
Answer: b) {True, 2}, 2.

Since True == 1 == 1.0 and they have the same hash, the set treats them as duplicates. Only the first one inserted (True) is kept. So the set has 2 elements: True and 2.
Q 03Multiple Choice
s = {[1, 2], [3, 4]}
Answer: b) TypeError.

Set elements must be hashable (just like dict keys). Lists are mutable and unhashable. Use frozensets or tuples as set elements: {(1,2), (3,4)} is valid. set() can accept any iterable of hashable items.
Set Operations
Q 04Predict the Output
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a | b)   # union
print(a & b)   # intersection
print(a - b)   # difference
print(a ^ b)   # symmetric difference
Output (order may vary)
{1, 2, 3, 4, 5, 6} {3, 4} {1, 2} {1, 2, 5, 6}
Why? Set operators: | = union (all elements). & = intersection (common elements). - = difference (in a but not b). ^ = symmetric difference (in either but not both). Sets are unordered — display order may vary.
Q 05Multiple Choice
a = {1, 2, 3}
b = {1, 2}
print(b.issubset(a))
print(a.issuperset(b))
print(a.isdisjoint({4, 5}))
Answer: a) True, True, True.

b.issubset(a): all of b's elements are in a → True. a.issuperset(b): a contains all of b → True. a.isdisjoint({4,5}): a and {4,5} share no elements → True. Alternative syntax: b <= a, a >= b.
frozenset
Q 06Predict the Output
fs = frozenset([1, 2, 3])
print(type(fs))
print(2 in fs)
d = {fs: "immutable set key"}
print(d[fs])
Output
<class 'frozenset'> True immutable set key
Why? frozenset is the immutable version of set. Because it's immutable, it's hashable — so it can be used as a dict key or set element. set cannot be used as a dict key (it's mutable and unhashable). frozenset supports all read-only set operations (union, intersection, etc.).
Q 07Multiple Choice
s = {1, 2, 3}
s.add(4)
s.add(2)    # already exists
print(len(s))
Answer: b) 4.

.add() adds an element to the set. If the element already exists, nothing happens — no error, no duplicate. So s.add(4) adds 4 (set becomes {1,2,3,4}), and s.add(2) does nothing (2 already there). Length stays 4.
Performance & Membership
Q 08Multiple Choice
big_list = list(range(1000000))
big_set = set(range(1000000))

# Which is faster?
999999 in big_list   # A
999999 in big_set    # B
Answer: b) B is dramatically faster.

List membership (in) is O(n) — it scans from the beginning. For a million elements, finding 999999 requires scanning all of them. Set membership is O(1) average — it uses a hash table and jumps directly to the value. For frequent membership testing, convert to a set first.
Q 09Predict the Output
nums = [1, 2, 3, 2, 1, 4]
unique = list(set(nums))
print(len(unique))
Output
4
Why? Converting to a set deduplicates automatically, then converting back to a list gives unique elements. Common idiom for removing duplicates. Caveat: set() doesn't preserve order — use dict.fromkeys() to deduplicate while preserving insertion order: list(dict.fromkeys(nums)).
Q 10Predict the Output
a = {1, 2, 3}
b = {1, 2, 3}
print(a == b)
print(a is b)
Output
True False
Why? Two sets with the same elements are equal (== is True). But they are different objects — is is False. This works correctly even though sets are unordered — == for sets checks element equality, not ordering.
Q 11Predict the Output
s = {1, 2, 3}
s.discard(2)
s.discard(99)   # doesn't exist
print(s)
Output
{1, 3}
Why? .discard(x) removes x if present, does nothing if absent (no error). Compare with .remove(x) which raises KeyError if x is not found. Use .discard() when you're not sure the element exists.
Q 12Multiple Choice
s = {3, 1, 4, 1, 5}
print(s[0])
Answer: c) TypeError.

Sets are unordered — there is no "first" element, so indexing is not supported. TypeError: 'set' object is not subscriptable. To get elements, iterate with for x in s, or convert to a sorted list: sorted(s).
← Chapter 08: Dictionaries Chapter 10: Classes & OOP →