Sets are unordered collection of data items. In a set, you can store multiple items in a single variable.
Sets are mutable, which means you can add or remove items after the set is created.
Sets do not allow duplicate items.
Creating Sets
Sets are defined using curly brackets {}
and items are separated by commas.
Example:
data = {"John", 11, 2, False, 3.9, 11}
print(data)
Output:
{False, 2, 3.9, 11, 'John'}
Here, you can see that the items of set occur in random order and hence they cannot be accessed using index numbers. Also sets do not allow duplicate values.
Creating Empty Set
data = {}
print(type(data))
# <class 'dict'>
In the above example, using empty curly brackets creates a dictionary not set. To create a empty set use set()
as follows:
data = set()
print(type(data))
# <class 'set'>
Set Comprehensions
Set comprehensions provide a concise way to create sets. They follow the same syntax as list comprehensions but use curly brackets instead of square brackets.
Syntax:
{expression for item in iterable if condition}
Example 1: Basic set comprehension
squares = {x**2 for x in range(5)}
print(squares) # Output: {0, 1, 4, 9, 16}
Example 2: Set comprehension with condition
even_squares = {x**2 for x in range(10) if x % 2 == 0}
print(even_squares) # Output: {0, 4, 16, 36, 64}
Accessing Set Items
Since indexing is not supported for sets, you can access items of set using a for
loop.
Example:
data = {"John", 11, 2, False, 3.9, 11}
for item in data:
print(item)
Output:
False
2
3.9
11
John
Check if an item exists in the set
You can check if an item exists in the set or not, using in
keyword.
Example:
info = {"Carla", 19, False, 5.9}
if "Carla" in info:
print("Carla is present.")
else:
print("Carla is absent.")
# Output: Carla is present.
Joining Sets
union() and update()
The union()
and update()
methods are used to print all items that are present in two sets.
The union()
method returns a new set whereas update()
method adds item into the existing set from another set.
Example 1:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.union(cities2)
print(cities3)
Output:
{'Tokyo', 'Madrid', 'Kabul', 'Seoul', 'Berlin', 'Delhi'}
Example 2:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.update(cities2)
print(cities)
Output:
{'Berlin', 'Madrid', 'Tokyo', 'Delhi', 'Kabul', 'Seoul'}
intersection() and intersection_update()
The intersection()
and intersection_update()
methods are used to print items that are common in both of the sets.
The intersection()
method returns a new set whereas intersection_update()
method updates the existing set from another set.
Example 1:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.intersection(cities2)
print(cities3)
Output:
{'Madrid', 'Tokyo'}
Example 2:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.intersection_update(cities2)
print(cities)
Output:
{'Tokyo', 'Madrid'}
symmetric_difference() and symmetric_difference_update()
The symmetric_difference()
and symmetric_difference_update()
methods are used to print items that are not similar in both of the sets.
The symmetric_difference()
method returns a new set whereas symmetric_difference_update()
method updates the existing set from another set.
Example 1:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.symmetric_difference(cities2)
print(cities3)
Output:
{'Seoul', 'Kabul', 'Berlin', 'Delhi'}
Example 2:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.symmetric_difference_update(cities2)
print(cities)
Output:
{'Kabul', 'Delhi', 'Berlin', 'Seoul'}
difference() and difference_update()
The difference()
and difference_update()
methods are used to print items that are only present in the original set and not in both of the sets.
The difference()
method returns a new set whereas difference_update()
method updates the existing set from another set.
Example 1:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Seoul", "Kabul", "Delhi"}
cities3 = cities.difference(cities2)
print(cities3)
Output:
{'Tokyo', 'Madrid', 'Berlin'}
Example 2:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Seoul", "Kabul", "Delhi"}
print(cities.difference(cities2))
Output:
{'Tokyo', 'Berlin', 'Madrid'}
Set Methods
Python provides following built-in methods for set manipulation:
isdisjoint()
The isdisjoint()
method checks if items of a given set are present in another set.
This method returns False
if items are present, else it returns True
.
Example:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
print(cities.isdisjoint(cities2)) # Output: False
issuperset()
The issuperset()
method checks if all the items of a particular set are present in the original set.
It returns True
if all the items are present, else it returns False
.
Example:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Seoul", "Kabul"}
print(cities.issuperset(cities2)) # Output: False
cities3 = {"Seoul", "Madrid","Kabul"}
print(cities.issuperset(cities3)) # Output: False
issubset()
The issubset()
method checks if all the items of the original set are present in the particular set.
It returns True
if all the items are present, else it returns False
.
Example:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Delhi", "Madrid"}
print(cities2.issubset(cities)) # Output: True
add()
The add()
method is used to add a single item to the set.
Example:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities.add("Helsinki")
print(cities) # Output: {'Tokyo', 'Helsinki', 'Madrid', 'Berlin', 'Delhi'}
update()
If you want to add more than one item, simply create another set or any other iterable object (list, tuple, dictionary), and use the update()
method to add it into the existing set.
Example:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Helsinki", "Warsaw", "Seoul"}
cities.update(cities2)
print(cities) # Output: {'Seoul', 'Berlin', 'Delhi', 'Tokyo', 'Warsaw', 'Helsinki', 'Madrid'}
remove()/discard()
The remove()
and discard()
methods are used to remove items from the set.
Example:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities.remove("Tokyo")
print(cities) # Output: {'Delhi', 'Berlin', 'Madrid'}
The main difference between remove()
and discard()
is that, if you try to remove an item which is not present in the set, then remove()
raises an error, whereas discard()
does not raise any error.
Example 1:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities.remove("Seoul")
print(cities) # KeyError: 'Seoul'
Example 2:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities.discard("Seoul")
print(cities) # Output: {'Madrid', 'Delhi', 'Berlin', 'Tokyo'}
pop()
The pop()
method is used to remove the last item of the set but the catch is that you don’t know which item gets popped as sets are unordered.
However, you can access the popped item if you assign the pop()
method to a variable.
Example:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
item = cities.pop()
print(cities)
print(item)
Output:
{'Tokyo', 'Delhi', 'Berlin'}
Madrid
del
del
is not a method, rather it is a keyword which is used to delete the set entirely.
Example:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
del cities
print(cities) # NameError: name 'cities' is not defined
You get an error because the entire set has been deleted and there is no variable called cities
which contains a set.
Now, if you don’t want to delete the entire set and just want to delete all items within that set then you can use the clear()
method.
clear()
The clear()
method is used to clear all items in the set and prints an empty set.
Example:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities.clear()
print(cities) # Output: set()
Frozenset
A frozenset is an immutable version of a set. Once created, you cannot add or remove items from it.
This makes frozenset hashable, so it can be used as a key in dictionaries or stored inside other sets.
Creating a frozenset:
data = frozenset([1, 2, 3, 4, 4])
print(data) # Output: frozenset({1, 2, 3, 4})
Example:
a = frozenset([1, 2, 3])
b = frozenset([2, 3, 4])
print(a.union(b)) # Output: frozenset({1, 2, 3, 4})
print(a.intersection(b)) # Output: frozenset({2, 3})
print(a.difference(b)) # Output: frozenset({1})
Example: Using frozenset as dictionary keys
# Regular sets cannot be used as dictionary keys
# frozensets can be used as dictionary keys
dict_with_frozenset_keys = {
frozenset([1, 2]): "first set",
frozenset([3, 4]): "second set"
}
print(dict_with_frozenset_keys) # Output: {frozenset({1, 2}): 'first set', frozenset({3, 4}): 'second set'}
Note: Frozensets support all set operations that don't modify the set (like union()
, intersection()
, difference()
), but don't support methods that would change the set (like add()
, remove()
, update()
).
👉 Next tutorial: Python Dictionaries