Python Foundation
Sets in Python
What is a Set?
A set is an unordered collection of unique items in Python.
Sets automatically remove duplicate values and do not maintain any order.
Python
fruits = {"apple", "banana", "mango", "apple"}
print(fruits)
print(type(fruits))
Output
{'apple', 'banana', 'mango'}
<class 'set'>
Key Characteristics
- Unordered → No indexing or slicing
- Unique → Duplicates are automatically removed
- Mutable → You can add or remove items
- Items must be immutable (numbers, strings, tuples)
Creating Sets
Using Curly Braces
Python
numbers = {10, 20, 30, 20, 10}
print(numbers)
Output
{10, 20, 30}
Using set() Constructor
Python
colors = set(["red", "green", "blue", "red"])
print(colors)
empty_set = set() # Correct way to create empty set
print(type(empty_set))
not_set = {} # This creates an empty dictionary
print(type(not_set))
Output
{'red', 'green', 'blue'}
<class 'set'>
<class 'dict'>
Adding Items to a Set
add() – Add single item
Python
cities = {"Hyderabad", "Mumbai"}
cities.add("Chennai")
print(cities)
Output
{'Hyderabad', 'Mumbai', 'Chennai'}
update() – Add multiple items
Python
cities.update(["Delhi", "Pune"])
print(cities)
Output
{'Hyderabad', 'Mumbai', 'Chennai', 'Delhi', 'Pune'}
Removing Items from a Set
Python
nums = {10, 20, 30, 40, 50}
nums.remove(30) # Raises error if item not found
print(nums)
nums.discard(100) # No error if item not found
print(nums)
nums.pop() # Removes a random item
print(nums)
nums.clear() # Removes all items
print(nums)
Output
{10, 20, 40, 50}
{10, 20, 40, 50}
{20, 40, 50}
set()
remove() raises error if item is missing. discard() is safer.
Set Operations
Sets support powerful mathematical operations.
Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print("Union:", a | b)
print("Intersection:", a & b)
print("Difference:", a - b)
print("Symmetric Difference:", a ^ b)
Output
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Difference: {1, 2}
Symmetric Difference: {1, 2, 5, 6}
Using Methods
Python
print(a.union(b))
print(a.intersection(b))
print(a.difference(b))
print(a.symmetric_difference(b))
Important Set Methods
Python
x = {1, 2, 3}
y = {1, 2, 3, 4, 5}
print(x.issubset(y)) # True if x is subset of y
print(y.issuperset(x)) # True if y is superset of x
print(x.isdisjoint({7, 8})) # True if no common elements
print(len(y))
print(3 in x)
print(10 not in x)
Output
True True True 5 True True
Frozen Set
A frozenset is an immutable version of a set. Once created, you cannot add or remove items.
Python
fs = frozenset([1, 2, 3, 2])
print(fs)
print(type(fs))
# fs.add(4) → AttributeError
# fs.remove(2) → AttributeError
Output
frozenset({1, 2, 3})
<class 'frozenset'>
Frozen sets can be used as dictionary keys or elements of another set.
When to Use Sets?
- When you need to store unique values only
- To remove duplicates from a list quickly
- To perform mathematical set operations (union, intersection, difference)
- For fast membership testing (
inoperator is very fast with sets)
Removing Duplicates from a List
Python
numbers = [1, 2, 2, 3, 4, 4, 5, 1]
unique_numbers = list(set(numbers))
print(unique_numbers)
Output
[1, 2, 3, 4, 5]
Summary – Sets in Python
- Set is an unordered collection of unique items.
- Created using
{ }orset(). - Duplicates are automatically removed.
- Supports mathematical operations: union, intersection, difference.
- Useful methods:
add(),update(),remove(),discard(),pop(),clear() frozensetis the immutable version of a set.
Next Topic → Dictionaries in Python
Sets in Python
Learn unordered collections of unique items and powerful set operations.
Select any topic from the left sidebar to begin.
Start Learning →
