Andromeda
Note

Sets (Python)

Definition

A collection of items in which every element must be unique. While defined using curly braces {} like dictionaries, sets do not contain key-value pairs.

Why It Matters

Sets in Python are the ‘efficiency hack’ for unique data; their extremely fast membership testing makes them indispensable for deduplication and complex filtering in large datasets where performance is critical.

Core Concepts

  • Uniqueness: Adding a duplicate item to a set has no effect; the set only retains one instance.
  • Unordered: Sets do not maintain insertion order (unlike lists and modern dictionaries).
  • Membership Testing: Extremely efficient at checking if an item exists within the collection.
  • Set Operations: Supports mathematical operations like union, intersection, and difference.
numbers = {1, 2, 3, 3, 4} # {1, 2, 3, 4}
numbers.add(5)

# Efficient membership testing
if 3 in numbers:
    print("Found 3")

# Set operations
evens = {2, 4, 6}
print(numbers.intersection(evens)) # {2, 4}

Connected Concepts