DATA STRUCTURES
Python Sets
The Ultimate Visual & Mathematical Guide to Python’s Unordered Collections
Core Characteristics
Sets are a fundamental Python data type designed for specific use cases where order doesn’t
matter, but uniqueness is paramount. They only accept immutable data types (like numbers,
strings, and tuples) as elements.
• Zero Duplicates: If you try to add a duplicate value, only one instance is stored. This is
perfect for filtering unique items dynamically.
• Unordered: Elements are not stored in any specific sequence. Because they lack a fixed
order, you cannot access items using index brackets (e.g., my_set[0]) or keys.
• Mutable Container, Immutable Elements: While the set itself can be changed (adding/re-
moving items), the elements placed inside it must belong to immutable types.
Creating & Modifying Sets
When initializing and altering sets in Python, there are a few syntax details to keep in mind:
# Creating a set with initial elements
my_set = {1, 2, 3}
# Empty Set Trap!
wrong_way = {} # This creates an empty dictionary (dict)
right_way = set () # This safely creates an empty set
Removal Methods
Python provides three primary methods to remove elements, each handling missing values
differently:
.discard(x) Removes the element x if present. If x is not found, it completes silently without
raising any errors.
.remove(x) Removes the element x. If x is not found in the set, it raises a KeyError.
.clear() Wipes the entire set clean, returning it to an empty state of set().
1
Mathematical Set Operations
Sets shine when comparing groups of data. Below is how common mathematical concepts map
to Python operators:
Operation Operator Description
Union my_set | your_set Returns all unique elements present in either set.
Intersection my_set & your_set Returns only elements shared by both sets.
Difference my_set - your_set Returns elements in the first set that are not in the
second.
Symmetric Difference my_set ^ your_set Returns elements in either set, but excluding what
they share.
Table 1: Core Set Mathematics in Python
my_set = {1, 2, 3}
your_set = {3, 4, 5}
# Examples of operations :
union_result = my_set | your_set # {1, 2, 3, 4, 5}
intersection_result = my_set & your_set # {3}
difference_result = my_set - your_set # {1, 2}
symmetric_diff = my_set ^ your_set # {1, 2, 4, 5}
2
Data Structure Comparison
To understand when to choose a Set over other built-in collection types, it helps to review how
their structural features compare:
Feature Sets Lists Tuples
Uniqueness Enforced (No dupli- Allows duplicates Allows duplicates
cates)
Ordering Unordered Ordered Ordered
Mutability Mutable Mutable Immutable
Indexing No Yes Yes
Average Search Time O(1) (Hash table O(n) (Linear O(n) (Linear
lookup) search) search)
Table 2: Sets vs. Lists vs. Tuples comparison table
Boolean Logic Checks
These methods allow you to quickly evaluate relationships between sets, returning a boolean
True or False:
• . issubset (other) : Checks if all elements of the target set are contained within the other
set.
• .issuperset(other): Checks if the target set contains every single element from the other
set.
• .isdisjoint(other): Evaluates whether the two sets share zero elements. Returns True if
they are entirely disjoint.
• x in my_set: The membership operator. Evaluates almost instantaneously (O(1) average
complexity) compared to lists (O(n) complexity).