A set in Python is an unordered, mutable, and unindexed collection of unique elements.
Sets are used when you want to store multiple items without duplicates, and order doesn’t matter.
my_set = {1, 2, 3, 4, 5}Key Properties:
- Unordered: Elements have no defined order
- Mutable: You can add or remove items
- Unique: Duplicate elements are automatically removed
- Unindexed: Elements cannot be accessed using indexes
# Creating a set
my_set = {1, 2, 3, 4, 5}
print(my_set)Output:
{1, 2, 3, 4, 5}
Use set(), not {}, because {} creates an empty dictionary.
empty_set = set()
print(empty_set)Output:
set()
Sets can contain different immutable data types.
mixed_set = {1, "apple", (2, 3)}
print(mixed_set)Output:
{1, 'apple', (2, 3)}
Invalid Example:
# invalid_set = {1, [2, 3]} # Raises TypeErrorError:
TypeError: unhashable type: 'list'
Reason → Lists are mutable, and mutable types cannot be set elements.
| Method | Description |
|---|---|
add(item) |
Adds an element to the set |
remove(item) |
Removes the element (raises error if not found) |
discard(item) |
Removes element if it exists (no error if not) |
pop() |
Removes a random element |
clear() |
Removes all elements |
my_set = {1, 2, 3, 4, 5}
my_set.add(6)
print(my_set)Output:
{1, 2, 3, 4, 5, 6}
my_set.remove(3)
print(my_set)Output:
{1, 2, 4, 5, 6}
Using discard() avoids errors:
my_set.discard(10)popped_item = my_set.pop()
print(popped_item)
print(my_set)Output (may vary):
1
{2, 4, 5, 6}
my_set.clear()
print(my_set)Output:
set()
Combines all elements from both sets, removing duplicates.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)Output:
{1, 2, 3, 4, 5}
You can also use:
print(set1 | set2)Returns elements present in both sets.
print(set1.intersection(set2))
print(set1 & set2)Output:
{3}
Elements that exist in the first set but not in the second.
print(set1.difference(set2))
print(set1 - set2)Output:
{1, 2}
Elements that are in either set, but not both.
print(set1.symmetric_difference(set2))
print(set1 ^ set2)Output:
{1, 2, 4, 5}
| Method | Description | Example | Output |
|---|---|---|---|
issubset() |
Checks if a set is a subset of another | {1,2}.issubset({1,2,3}) |
True |
issuperset() |
Checks if a set is a superset | {1,2,3}.issuperset({1,2}) |
True |
isdisjoint() |
Checks if sets have no common elements | {1,2,3}.isdisjoint({4,5,6}) |
True |
Creates a shallow copy of a set.
set1 = {1, 2, 3}
copy_set = set1.copy()
print(copy_set)Output:
{1, 2, 3}
Since sets are unordered, elements may appear in random order.
my_set = {1, 2, 3, 4, 5}
for element in my_set:
print(element)Output (order may vary):
1
2
3
4
5
A frozenset is an immutable version of a set — its contents cannot be changed.
frozen_set = frozenset([1, 2, 3, 4])
print(frozen_set)Output:
frozenset({1, 2, 3, 4})
Attempting to modify it will raise an error:
# frozen_set.add(5)Error:
AttributeError: 'frozenset' object has no attribute 'add'
A common use-case of sets is to remove duplicates from lists.
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_set = set(my_list)
print(unique_set)Output:
{1, 2, 3, 4, 5}
| Error | Cause | Example |
|---|---|---|
TypeError |
Adding mutable elements (like lists/dicts) | {[1,2,3]} |
KeyError |
Removing non-existent element with .remove() |
{1,2}.remove(5) |
AttributeError |
Modifying a frozenset |
frozenset({1,2}).add(3) |
| Feature | Description |
|---|---|
| Unordered | Elements do not have a fixed position |
| Mutable | You can add or remove elements |
| Unique | No duplicate elements allowed |
| Unindexed | Cannot access elements by index |
| Supports Operations | Union, intersection, difference, etc. |
| Immutable Version | frozenset |
- Create a set of favorite movies and add a new one using
.add(). - Find the intersection of
{1, 2, 3}and{3, 4, 5}using both methods (&and.intersection()). - Remove duplicates from
[10, 20, 10, 30, 20, 40]using a set. - Check if
{1, 2}is a subset of{1, 2, 3, 4}. - Create a
frozensetand attempt to add a new element — observe the error.