Set
An unordered collection of unique items, written in braces, e.g. {1, 2, 3}.
A set stores unique values — no duplicates — with no fixed order. It is built for fast membership tests (x in s) and set maths: union, intersection, and difference. Create one with braces or set(...). Note that {} makes an empty dictionary, so use set() for an empty set.
emails = ["[email protected]", "[email protected]", "[email protected]"]
unique = set(emails) # duplicates dropped
print(len(unique))
print("[email protected]" in unique) # fast membership test
Output
2 True
Where this shows up in real Python
Sets are perfect for removing duplicates, testing membership quickly, and comparing two collections — who is in list A but not list B.
Commonly used Set tools
.add(x)— add an item.discard(x)— remove without error if missinga | b— union — items in eithera & b— intersection — items in botha - b— difference — in a but not bset(items)— drop duplicates from a list
Official documentation: Python Library Reference: Set Types