Lesson 12 / 47

Sets

Unordered collections of unique items, built for membership and set math.

Unique & fast lookups

A set automatically drops duplicates and checks membership in near-constant time.

tags = {"python", "web", "python"}
print(tags)          # {'python', 'web'}
print("web" in tags) # True

Set operations

Sets support math operations: union, intersection, difference — handy for comparing groups.

a = {1, 2, 3}
b = {2, 3, 4}
print(a | b)   # union {1,2,3,4}
print(a & b)   # intersection {2,3}
print(a - b)   # difference {1}