Unordered, unindexed, no duplicates. Sets are hash tables — elements must be hashable. Understand frozenset, set operations, and when sets beat lists.
{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.s = {1, 1, 2, 2, 3}
print(s)
print(len(s))
print(type({}))
{1,1,2,2,3} becomes {1,2,3}. Important: {} creates an empty dict, not a set. To get an empty set: set().s = {True, 1, 1.0, 2}
print(s)
print(len(s))
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.s = {[1, 2], [3, 4]}
{(1,2), (3,4)} is valid. set() can accept any iterable of hashable items.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
| = 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.a = {1, 2, 3}
b = {1, 2}
print(b.issubset(a))
print(a.issuperset(b))
print(a.isdisjoint({4, 5}))
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.fs = frozenset([1, 2, 3]) print(type(fs)) print(2 in fs) d = {fs: "immutable set key"} print(d[fs])
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.).s = {1, 2, 3}
s.add(4)
s.add(2) # already exists
print(len(s))
.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.big_list = list(range(1000000)) big_set = set(range(1000000)) # Which is faster? 999999 in big_list # A 999999 in big_set # B
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.nums = [1, 2, 3, 2, 1, 4] unique = list(set(nums)) print(len(unique))
set() doesn't preserve order — use dict.fromkeys() to deduplicate while preserving insertion order: list(dict.fromkeys(nums)).a = {1, 2, 3}
b = {1, 2, 3}
print(a == b)
print(a is b)
== 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.s = {1, 2, 3}
s.discard(2)
s.discard(99) # doesn't exist
print(s)
.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.s = {3, 1, 4, 1, 5}
print(s[0])
TypeError: 'set' object is not subscriptable. To get elements, iterate with for x in s, or convert to a sorted list: sorted(s).