Sets in Python
A set is a collection of items that is unordered and contains only unique values. Sets are useful whenever you need to guarantee that a value only appears once, or when you need to quickly check whether an item exists in a collection.
Creating a Set
A set is written using curly braces {} with comma-separated values.
colors = {"red", "green", "blue"}
print(colors)
print(type(colors)) # <class 'set'>
Unordered and Unique
A set does not keep track of insertion order or position, so items cannot be accessed by index. More importantly, a set automatically removes duplicates. Adding a value that already exists in the set has no effect at all.
numbers = {1, 2, 3, 2, 1}
print(numbers) # {1, 2, 3} - duplicates were dropped automatically
colors = {"red", "green", "blue"}
colors.add("red") # "red" is already in the set
print(colors) # still {'red', 'green', 'blue'} - no effect, no error
print(len(colors)) # 3
Note: Because sets are unordered, the printed order of items may not match the order you typed them in, and it can vary between runs.
Common Set Methods
Sets provide methods for adding, removing, and combining values.
| Method | Description |
|---|---|
.add(value) | Adds a single item to the set. |
.remove(value) | Removes an item; raises KeyError if it is not present. |
.discard(value) | Removes an item if present; does nothing if it is not present (no error). |
.union(other) | Returns a new set with all items from both sets. |
.intersection(other) | Returns a new set with only the items common to both sets. |
.difference(other) | Returns a new set with items in the first set that are not in the second. |
a = {1, 2, 3}
b = {3, 4, 5}
a.add(10)
print(a) # {1, 2, 3, 10}
a.discard(10)
print(a) # {1, 2, 3}
print(a.union(b)) # {1, 2, 3, 4, 5}
print(a.intersection(b)) # {3}
print(a.difference(b)) # {1, 2}
Set Operators
The same union, intersection, and difference operations can also be written with operators, which many developers find more readable.
| Operator | Meaning |
|---|---|
| | Union - items in either set |
& | Intersection - items in both sets |
- | Difference - items in the first set but not the second |
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5} same as a.union(b)
print(a & b) # {3} same as a.intersection(b)
print(a - b) # {1, 2} same as a.difference(b)
Why Sets Are Useful
Two practical, everyday uses for sets:
- Fast membership testing: checking
value in my_setis much faster than checkingvalue in my_listfor large collections. - Removing duplicates from a list: converting a list to a set and back is a common one-line trick to deduplicate values.
names = ["Amit", "Neha", "Amit", "Ravi", "Neha"]
unique_names = list(set(names))
print(unique_names) # order not guaranteed, e.g. ['Amit', 'Neha', 'Ravi']
# fast membership test
allowed_users = {"admin", "editor", "viewer"}
if "editor" in allowed_users:
print("Access granted")
The Empty Set Gotcha
Writing {} does not create an empty set — it creates an empty dictionary. To create an empty set, you must use the set() constructor instead.
not_a_set = {}
print(type(not_a_set)) # <class 'dict'>
empty_set = set()
print(type(empty_set)) # <class 'set'>
Note: This is one of the most common beginner mistakes with sets. Whenever you need an empty set, always use set() explicitly.