Python Sets

Tutorial 12 of 65 · pythondeck.com Python course

Sets are unordered collections of unique, hashable items. They support fast membership tests and mathematical operations: union (|), intersection (&), difference (-) and symmetric difference (^). frozenset is the immutable variant.

Sets are unordered collections of unique hashable elements. They excel at membership tests, deduplication, and mathematical set operations (union, intersection) in average O(1) time.

When order matters or duplicates count, use list or dict; when you only care about whether something was seen, use a set. In data cleaning, set(items) quickly drops duplicate tags or IDs before you persist results. Permission systems compare role sets with intersection.

Literals: {1, 2, 3}; empty set: set() not {} (that is a dict).

Add/remove: add, discard, remove (KeyError if missing).

Algebra: | union, & intersection, - difference, ^ symmetric difference.

Set comprehensions: {x.lower() for x in words}.

frozenset is immutable and hashable for nested structures.

Elements must be hashable—no lists as members.

Sets lose insertion order historically; Python 3.7+ preserves insertion order for dicts, and sets are officially unordered—do not rely on iteration order for logic.

Counting unique items: len(set(items)). Finding duplicates: compare len(set) vs len(list).

Subset tests use <=; for per-value counts prefer Counter over a plain set.

Writing {} expecting an empty set.

Putting unhashable dicts or lists into a set.

Using set when you needed multiset counts—use collections.Counter.

Modifying a set while iterating it—iterate over a copy or collect deletes first.

Build sets from comprehensions for unique transformed values.

Use set operations for permission checks and tag intersections instead of nested loops.

Convert to sorted list only for display: sorted(my_set).

Use frozenset for keys in dicts mapping sets to values.

Build intersections with & instead of nested loops when comparing large tag sets.

Re-read the examples below with these ideas in mind; change variable names and inputs to match your own project.

The program below demonstrates deduplicate. Read the comments on each line, run the code, then change names or values to see how the output shifts.

# Example: Deduplicate
# Run in the REPL or save as a .py file and execute with python.
nums = [1, 2, 2, 3, 3, 3, 4]
print(set(nums))

This sample walks through set math in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.

# Example: Set math
# Run in the REPL or save as a .py file and execute with python.
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a & b, a | b, a - b, a ^ b)

Here is a hands-on illustration of set comprehension. Follow the inline comments first; only then execute the snippet and compare the result with what you expected.

# Example: Set comprehension
# Run in the REPL or save as a .py file and execute with python.
vowels = {c for c in "comprehension" if c in "aeiou"}
print(vowels)

The program below demonstrates set algebra. Read the comments on each line, run the code, then change names or values to see how the output shifts.

# Sets store unique hashable elements with math-like operators
a = {1, 2, 3, 4}  # curly braces build a set
b = {3, 4, 5, 6}  # overlapping elements with a
print(a | b)  # union -> {1..6}
print(a & b)  # intersection -> {3, 4}
print(a - b)  # difference -> {1, 2}
print(a ^ b)  # symmetric difference -> {1,2,5,6}
a.add(9)  # insert single element
a.discard(99)  # remove if present; no KeyError
print(sorted(a))  # sets are unordered; sort for display

This sample walks through dedup pipeline in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.

# Order-preserving dedupe using dict.fromkeys (3.7+)
raw = ["a", "b", "a", "c", "b", "d"]  # duplicates
unique = list(dict.fromkeys(raw))  # keeps first occurrence order
print(unique)  # ['a','b','c','d']
tags = {"python", "deck", "python", "tutorial"}  # literal dedupes
print(len(tags), tags)  # 3 unique strings
frozen = frozenset([1, 2, 3])  # immutable set
print(2 in frozen)  # membership test
combo = frozen | {3, 4}  # union with new set
print(combo)  # frozenset({1,2,3,4})

« Python Tuples All tutorials Python Dictionaries »