Table of Contents
What is a Set?
In Python, a set is an unordered collection of unique elements.
Key properties:
- No duplicates: If you add the same value twice, it appears only once.
- Unordered: A set does not keep the order of items; you can’t rely on positions (no indexing like
my_set[0]). - Mutable: You can add or remove elements from a set after creating it.
- Elements must be hashable: In practice, you usually store numbers, strings, or tuples in sets (not lists or other sets).
Typical uses:
- Removing duplicates from a list.
- Checking if something is a member of a collection, very efficiently.
- Doing math-like operations such as union and intersection (e.g. “items that are in both groups”).
Creating Sets
Using curly braces
You can create a set using {} with comma-separated values:
fruits = {"apple", "banana", "cherry"}
print(fruits)Remember: the order of printing may be different from how you wrote it.
Using the `set()` function
You can also create a set using the set() function:
numbers = set([1, 2, 3])
print(numbers)This is especially useful for converting other collections:
- From list to set:
nums_list = [1, 2, 2, 3, 3, 3]
nums_set = set(nums_list)
print(nums_set) # duplicates removed- From string to set (characters):
letters = set("hello")
print(letters) # each unique character onceEmpty set
Be careful: {} creates an empty dictionary, not a set.
To create an empty set, use:
empty_set = set()
print(empty_set)Uniqueness and Duplicates
Sets automatically remove duplicates:
numbers = {1, 2, 2, 3, 3, 3}
print(numbers) # {1, 2, 3}If you add the same element multiple times, it only appears once:
colors = set()
colors.add("red")
colors.add("red")
colors.add("blue")
print(colors) # {"red", "blue"}You can use this property to quickly remove duplicates from a list:
names = ["Alice", "Bob", "Alice", "Charlie", "Bob"]
unique_names = set(names)
print(unique_names)If you need a list again, convert it back:
unique_names_list = list(unique_names)
print(unique_names_list)Basic Set Operations
Membership: checking if something is in a set
Use the in and not in operators:
animals = {"cat", "dog", "bird"}
print("cat" in animals) # True
print("fish" in animals) # False
print("fish" not in animals) # TrueMembership checks in sets are typically faster than in lists, especially when the collection is large.
Adding elements: `add()`
Use add() to insert a single element:
numbers = {1, 2, 3}
numbers.add(4)
print(numbers)If the element already exists, nothing changes:
numbers.add(3) # 3 is already in the set
print(numbers) # still {1, 2, 3, 4}Removing elements: `remove()` and `discard()`
remove(x)removesx, but causes an error ifxis not in the set.discard(x)removesxif present, and does nothing ifxis absent.
numbers = {1, 2, 3}
numbers.remove(2)
print(numbers) # {1, 3}
numbers.discard(5) # 5 is not in the set, but no error
print(numbers) # still {1, 3}
If you’re unsure whether an element is present, discard() is safer.
Removing and returning an element: `pop()`
pop() removes and returns some element from the set. Since sets are unordered, you don’t control which one:
items = {"apple", "banana", "cherry"}
item = items.pop()
print("Popped:", item)
print("Remaining:", items)Clearing all elements: `clear()`
To empty a set:
data = {1, 2, 3}
data.clear()
print(data) # set()Set Operations (Like Math Sets)
Sets support operations similar to mathematical sets.
Given:
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}Union: `|` or `union()`
Union combines elements that are in either set.
$$
A \cup B = \{x : x \in A \text{ or } x \in B\}
$$
In Python:
print(a | b) # {1, 2, 3, 4, 5, 6}
print(a.union(b)) # same resultIntersection: `&` or `intersection()`
Intersection keeps elements that are in both sets.
$$
A \cap B = \{x : x \in A \text{ and } x \in B\}
$$
In Python:
print(a & b) # {3, 4}
print(a.intersection(b)) # same resultDifference: `-` or `difference()`
Difference keeps elements that are in the first set but not in the second.
$$
A \setminus B = \{x : x \in A \text{ and } x \notin B\}
$$
In Python:
print(a - b) # {1, 2}
print(b - a) # {5, 6}
print(a.difference(b)) # {1, 2}Symmetric difference: `^` or `symmetric_difference()`
Symmetric difference keeps elements that are in either set, but not in both.
$$
A \triangle B = (A \cup B) \setminus (A \cap B)
$$
In Python:
print(a ^ b) # {1, 2, 5, 6}
print(a.symmetric_difference(b)) # same resultSubsets and Supersets (Basics)
For simple checks of “is everything in this set also in that set?” you can use:
<=(subset)>=(superset)
Example:
small = {1, 2}
big = {1, 2, 3, 4}
print(small <= big) # True: small is a subset of big
print(big >= small) # True: big is a superset of smallWhen to Use Sets (Practical Ideas)
Some beginner-friendly scenarios:
- Remove duplicates from data:
emails = ["a@example.com", "b@example.com", "a@example.com"]
unique_emails = set(emails)- Fast membership checks (e.g. words you want to ignore):
stop_words = {"the", "and", "is", "of"}
word = "and"
if word in stop_words:
print("Skip this word")- Comparing groups (e.g. students in different classes):
class_a = {"Alice", "Bob", "Carol"}
class_b = {"Bob", "Dave"}
both = class_a & class_b # in both classes
only_a = class_a - class_b # only in class A
everyone = class_a | class_b # all unique studentsLimitations and Things to Remember
- Sets are unordered: you cannot rely on item order, and you cannot use indexes like
my_set[0]. - Set elements must be immutable (e.g. numbers, strings, tuples are fine; lists are not).
- There is a separate type called
frozenset(an immutable set), but that goes beyond the basics.
Understanding these basics will help you decide when a set is more appropriate than a list, tuple, or dictionary in your programs.