Lesson 9 · Python data types deep dive
Python Sets Explained: Union, Intersection & Difference | Data Types #9
Video nine of the twelve-part series: set, Python's mutable collection of unique, unordered elements. Construction, all seventeen public set methods, the…
- CoursePython data types deep dive
- Lesson9 of 12
- Video18 min
- FormatJupyter notebook · 13 code cells
What you'll learn
- What Makes set Different
- Set Literals and Construction
- Adding Elements: add() and update()
- Removing Elements: remove(), discard(), pop(), clear()
- Membership and Comparison: isdisjoint(), issubset(), issuperset()
- Combining Sets: union(), intersection(), difference(), symmetricdifference()
- In-Place Variants: intersectionupdate(), differenceupdate(), symmetricdifferenceupdate()
- copy()
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbPython Data Types Deep-Dive, Video 9: Sets (set)#
- Video nine of the twelve-part series: set, Python's mutable collection of unique, unordered elements.
- Construction, all seventeen public set methods, the equivalent operators, and set comprehensions.
- Let's get into it.
Part 1: What Makes set Different#
numbers = {1, 2, 3, 2, 1}
print(numbers)
print(type(numbers))
print(len(numbers))
try:
numbers[0]
except TypeError as e:
print(f'Caught: {e}')
Part 2: Set Literals and Construction#
looks_empty = {}
print(type(looks_empty))
genuine_empty_set = set()
print(type(genuine_empty_set))
from_list = set([1, 2, 2, 3])
from_string = set('mississippi')
from_tuple = set((1, 2, 3))
print(from_list, from_string, from_tuple)
Part 3: Adding Elements: add() and update()#
colors = {'red', 'green'}
colors.add('blue')
print(colors)
colors.add('red')
print(colors)
colors.update(['yellow', 'purple'])
print(colors)
colors.update({'orange'}, ['pink', 'gray'])
print(colors)
Part 4: Removing Elements: remove(), discard(), pop(), clear()#
colors = {'red', 'green', 'blue'}
colors.remove('green')
print(colors)
try:
colors.remove('purple')
except KeyError as e:
print(f'Caught: {e}')
colors.discard('purple')
print(colors)
colors.discard('red')
print(colors)
numbers = {5, 1, 4, 2, 3}
removed = numbers.pop()
print(removed, numbers)
numbers.clear()
print(numbers)
Part 5: Membership and Comparison: isdisjoint(), issubset(), issuperset()#
a = {1, 2, 3}
b = {4, 5, 6}
c = {2, 3}
print(a.isdisjoint(b))
print(a.isdisjoint(c))
print(c.issubset(a))
print(a.issubset(c))
print(a.issuperset(c))
print(c.issuperset(a))
print(c <= a)
print(a >= c)
Part 6: Combining Sets: union(), intersection(), difference(), symmetric_difference()#
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a.union(b))
print(a | b)
print(a.intersection(b))
print(a & b)
print(a.difference(b))
print(a - b)
print(b.difference(a))
print(a.symmetric_difference(b))
print(a ^ b)
a = {1, 2, 3}
b = {2, 3, 4}
c = {3, 4, 5}
print(a.union(b, c))
print(a.intersection(b, c))
Part 7: In-Place Variants: intersection_update(), difference_update(), symmetric_difference_update()#
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a.intersection_update(b)
print(a)
a = {1, 2, 3, 4}
a.difference_update(b)
print(a)
a = {1, 2, 3, 4}
a.symmetric_difference_update(b)
print(a)
Part 8: copy()#
original = {1, 2, 3}
alias = original
alias.add(4)
print(original)
print(original is alias)
real_copy = original.copy()
real_copy.add(5)
print(original)
print(real_copy)
print(original is real_copy)
Part 9: Set Comprehensions#
squares = {n ** 2 for n in range(-3, 4)}
print(squares)
words = ['apple', 'Apple', 'BANANA', 'banana']
unique_lower = {w.lower() for w in words}
print(unique_lower)
letters = {c for c in 'mississippi' if c not in 'aeiou'}
print(letters)
Part 10: Practical Patterns#
raw_emails = ['a@x.com', 'b@x.com', 'a@x.com', 'c@x.com', 'b@x.com']
unique_emails = list(set(raw_emails))
print(sorted(unique_emails))
print(len(raw_emails), len(unique_emails))
import timeit
big_list = list(range(100000))
big_set = set(range(100000))
list_time = timeit.timeit(lambda: 99999 in big_list, number=1000)
set_time = timeit.timeit(lambda: 99999 in big_set, number=1000)
print(f'List membership check: {list_time:.4f}s')
print(f'Set membership check: {set_time:.4f}s')
print(f'Set is roughly {list_time / set_time:.0f}x faster here')
Wrap-Up: What You Learned#
- set holds unique, unordered, hashable elements, and is mutable.
- Construction: literals, the set() constructor (and the empty-braces-means-dict trap).
- Adding and removing: add, update, remove, discard, pop, clear.
- Comparisons: isdisjoint, issubset, issuperset, and their operator shorthand.
- Combining sets: union, intersection, difference, symmetric_difference, and their operators.
- In-place variants: intersection_update, difference_update, symmetric_difference_update.
- copy(), set comprehensions, and two genuinely practical real-world use cases.
- That's all seventeen public set methods covered. Next up: frozenset, set's immutable, hashable cousin.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



