Chapter 6: Sets
Explore sets, the unique and unordered collections in Python. Learn how to filter duplicates, mutate elements, perform union & intersection, and visualize relationships with Venn diagrams.
Why Do We Need Sets?
When working with collections of data, we often need to guarantee that every item is unique (such as unique visitor IDs), or we want to perform mathematical operations like finding common values (intersection) or combining groups (union). Python's Sets offer unordered, unique collections optimized for fast membership checking and set algebra.
Let's explore how sets are created, structured, and managed in Python.
6.1 What is a Set?
A Set is a collection of elements enclosed in curly brackets {}. Unlike lists and tuples, sets are unordered (meaning they do not record the position of elements) and only contain unique elements (duplicate elements are automatically filtered out).
Try running this code block to verify that duplicate strings are collapsed into unique values:
# Defining a set with duplicate items
A = {"AC/DC", "Back in Black", "AC/DC", "Thriller"}
print("Resulting set A:", A)
Try It Yourself »
6.2 List to Set Typecasting
You can convert a list to a set using the set() constructor function. This operation is called typecasting, and is a common technique in Python to remove duplicates from an array sequence.
Type list elements separated by commas below. Click the button to watch how casting to a set identifies and filters out the duplicate values.
6.3 Mutating Sets: Add, Remove, & Membership
Sets are mutable. You can add items using the .add() method, remove items using the .remove() method, and verify membership using the in operator.
Perform adding, deleting, and checking operations on Set A. Watch what happens if you add duplicates!
Try running this mutability code block in the playground to check how duplicate additions are ignored:
# Set mutability operations
A = {"AC/DC", "Back in Black"}
A.add("NSYNC")
A.add("NSYNC") # Duplicate, will not change the set!
A.remove("NSYNC")
print("AC/DC in A?", "AC/DC" in A)
Try It Yourself »
6.4 Venn Diagram Simulator: Mathematical Operations
Venn Diagrams are shapes that visualize mathematical operations between sets:
- Intersection (
&): Finds elements that exist in both Set A and Set B (overlapping center region). - Union (
|): Combines all elements from both sets (both circles highlighted). - Difference (
-): Finds elements that exist in A but not in B (left crescent highlighted). - Symmetric Difference (
^): Finds elements in A or B, but not in both (both crescents, center empty).
Click on any set operation button below to see the math results and watch the Venn diagram light up the active regions!
Try running this set math example in the interactive playground:
# Set operations math
A = {"AC/DC", "Back in Black", "Thriller"}
B = {"Back in Black", "Thriller", "Dark Side", "The Wall"}
print("Intersection (A & B):", A & B)
print("Union (A | B):", A | B)
print("Difference (A - B):", A - B)
print("Symmetric Difference (A ^ B):", A ^ B)
Try It Yourself »
6.5 Subsets and Supersets
A set is a subset of another set if all of its elements are fully contained in the second set. You can check this relation using the .issubset() method (or `A <= B` operator). Similarly, the other set is the superset (checked with .issuperset() or `B >= A` operator).
Choose Set B's items below to dynamically see if Set B is fully encapsulated as a subset of Set A = {"AC/DC", "Back in Black"}.
Try running this subset check in the interactive playground:
# Subset check examples
A = {"AC/DC", "Back in Black"}
B = {"AC/DC"}
C = {"Thriller"}
print("B subset of A:", B.issubset(A))
print("C subset of A:", C.issubset(A))
Try It Yourself »
Practice Quiz
Test your knowledge of Python sets by answering the multiple-choice questions below.