Data Structures

Python Sets

Learn about Python sets - unordered collections of unique elements with set theory operations like union, intersection, and difference.

Python Sets

Sets are unordered collections of unique elements. They are perfect for removing duplicates, membership testing, and mathematical set operations like union, intersection, and difference.


Creating Sets

python
# With curly braces
fruits = {"apple", "banana", "cherry"}

# With set() constructor
numbers = set([1, 2, 3, 4, 5])
chars = set("hello")  # {'h', 'e', 'l', 'o'} - duplicates removed

# Empty set (NOT {} which creates a dict!)
empty = set()    # Correct
# empty = {}     # This creates a dict!

# Duplicates are automatically removed
nums = {1, 2, 2, 3, 3, 3, 4}
print(nums)  # {1, 2, 3, 4}

# From list (remove duplicates)
items = [1, 2, 2, 3, 3, 4]
unique = set(items)
print(unique)  # {1, 2, 3, 4}

Note: Sets are unordered β€” you cannot access elements by index.


Adding and Removing Elements

python
colors = {"red", "green", "blue"}

# add() - single element
colors.add("yellow")
print(colors)  # {'red', 'green', 'blue', 'yellow'}

# update() - multiple elements
colors.update(["purple", "orange"])
colors.update({"pink", "cyan"})
print(colors)

# discard() - remove (no error if missing)
colors.discard("red")
colors.discard("black")  # No error

# remove() - remove (raises error if missing)
colors.remove("green")
# colors.remove("black")  # KeyError!

# pop() - remove and return arbitrary element
item = colors.pop()
print(f"Removed: {item}")

# clear() - remove all
colors.clear()
print(colors)  # set()

Set Operations

Union (| or union())

All elements from both sets:

python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

print(a | b)         # {1, 2, 3, 4, 5, 6}
print(a.union(b))    # {1, 2, 3, 4, 5, 6}

Intersection (& or intersection())

Elements common to both sets:

python
print(a & b)               # {3, 4}
print(a.intersection(b))   # {3, 4}

Difference (- or difference())

Elements in first set but not in second:

python
print(a - b)             # {1, 2}
print(a.difference(b))   # {1, 2}
print(b - a)             # {5, 6}

Symmetric Difference (^ or symmetric_difference())

Elements in either set but not both:

python
print(a ^ b)                        # {1, 2, 5, 6}
print(a.symmetric_difference(b))    # {1, 2, 5, 6}

Visual Summary

A = {1, 2, 3, 4}    B = {3, 4, 5, 6}

A | B  = {1, 2, 3, 4, 5, 6}    Union
A & B  = {3, 4}                  Intersection
A - B  = {1, 2}                  Difference
B - A  = {5, 6}                  Difference
A ^ B  = {1, 2, 5, 6}           Symmetric Difference

Set Comparisons

python
a = {1, 2, 3}
b = {1, 2, 3, 4, 5}
c = {1, 2, 3}

# Subset: all elements of a are in b
print(a <= b)          # True
print(a.issubset(b))   # True

# Proper subset: subset but not equal
print(a < b)    # True
print(a < c)    # False (they're equal)

# Superset: b contains all elements of a
print(b >= a)            # True
print(b.issuperset(a))   # True

# Disjoint: no common elements
x = {1, 2, 3}
y = {4, 5, 6}
print(x.isdisjoint(y))   # True

Frozen Sets

Immutable sets that can be used as dictionary keys:

python
# Create frozen set
frozen = frozenset([1, 2, 3, 4, 5])
print(frozen)  # frozenset({1, 2, 3, 4, 5})

# Cannot modify
# frozen.add(6)  # AttributeError

# Can use as dictionary key
permissions = {
    frozenset(["read"]): "viewer",
    frozenset(["read", "write"]): "editor",
    frozenset(["read", "write", "admin"]): "admin",
}

user_perms = frozenset(["read", "write"])
print(permissions[user_perms])  # editor

# Set operations work on frozen sets
a = frozenset([1, 2, 3])
b = frozenset([2, 3, 4])
print(a | b)  # frozenset({1, 2, 3, 4})

Set Comprehensions

python
# Basic comprehension
squares = {x**2 for x in range(10)}
print(squares)  # {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}

# With condition
even_squares = {x**2 for x in range(10) if x % 2 == 0}
print(even_squares)  # {0, 4, 16, 36, 64}

# From string - unique characters
text = "mississippi"
unique_chars = {c for c in text}
print(unique_chars)  # {'m', 'i', 's', 'p'}

Common Use Cases

Remove Duplicates from a List

python
items = [1, 2, 2, 3, 3, 3, 4, 4, 5]
unique = list(set(items))
print(unique)  # [1, 2, 3, 4, 5]

# Preserve order (Python 3.7+)
unique_ordered = list(dict.fromkeys(items))
print(unique_ordered)  # [1, 2, 3, 4, 4, 5] - wrong, should be:
# Actually dict.fromkeys preserves order and removes dupes
print(unique_ordered)  # [1, 2, 3, 4, 5]

Fast Membership Testing

python
# Sets have O(1) lookup vs O(n) for lists
valid_codes = {"A100", "B200", "C300", "D400", "E500"}

user_code = "B200"
if user_code in valid_codes:  # Very fast!
    print("Valid code")

Find Common Elements

python
# Students in multiple courses
math_students = {"Alice", "Bob", "Charlie", "Diana"}
science_students = {"Bob", "Diana", "Eve", "Frank"}
english_students = {"Alice", "Charlie", "Eve", "Grace"}

# Students in all three courses
all_three = math_students & science_students & english_students
print(f"All three courses: {all_three}")  # set()

# Students in at least one course
any_course = math_students | science_students | english_students
print(f"Any course: {any_course}")

# Only in math
only_math = math_students - science_students - english_students
print(f"Only math: {only_math}")  # {'Bob'} or similar

Summary

  • Sets are unordered collections of unique elements
  • Create with {1, 2, 3} or set() β€” empty set must use set()
  • Duplicates are automatically removed
  • Set operations: | (union), & (intersection), - (difference), ^ (symmetric difference)
  • O(1) membership testing makes sets ideal for lookups
  • Use frozenset for immutable sets (usable as dict keys)
  • Set comprehensions work like list comprehensions: {x**2 for x in range(10)}
  • Common uses: deduplication, membership testing, finding commonalities

Next, we'll learn about Python list comprehensions in depth.