Skip to content

Python Set

How to create, modify, and perform mathematical operations on Python sets, including union, intersection, difference, and more.

A Python set is an unordered collection of elements. Every element is unique (i.e., no duplicates) and must be immutable (i.e., can't be changed). The elements of a set are unordered.

However, a set itself is mutable because elements can be added or removed from it.

In addition, it is possible to perform mathematical operations on sets, such as union, intersection, symmetrical difference, etc.

Create Python Set

A set is created by placing all the elements inside the curly brackets {}, separated by a comma, or by using the built-in set() function.

A set can have any number of elements of different types (integer, float, tuple, string etc.). But a set can't contain mutable elements like lists, sets or dictionaries as its elements.

my_set = {1, 2, 3}
print(my_set) # {1, 2, 3}

my_set = {1.0, "a string", (1, 2, 3)}
print(my_set) # {1.0, (1, 2, 3), 'a string'}

The sets have unique values, so duplicates are deleted automatically.

my_set = {1, 2, 3, 4, 3, 2}
print(my_set) # {1, 2, 3, 4}

Create a Set from a List

In addition, it is possible to create a set from a list

my_set = set([1, 2, 3, 2])
print(my_set) # {1, 2, 3}

Create an Empty Set

It's tricky because empty curly braces {} will make an empty dictionary in Python.

To create a set without elements, it is necessary to use the set() function without any arguments.


a = {}

print(type(a)) # <class 'dict'>

a = set()

print(type(a)) # <class 'set'>

Access Set Elements

Since sets are unordered collections, indexing has no meaning. Therefore, the slicing operator [] does not work.

Therefore, it is not possible to access or modify an element of a set using indexing or slicing.

Modify a set

You can add a single element with the add() method and multiple elements with the update() method.

my_set = {1, 3}
print(my_set) # {1, 3}


my_set.add(2)
print(my_set) # {1, 2, 3}

my_set.update([2, 3, 4])
print(my_set) # {1, 2, 3, 4}

my_set.update([4, 5], {1, 6, 8})
print(my_set) # {1, 2, 3, 4, 5, 6, 8}

Remove elements from a set

with discard() and remove()

An element can be removed from a set using discard() and remove() methods.

  • The discard() function leaves a set unchanged if the element is not present in the set.
  • The remove() function will raise an error if the element is not present in the set.
my_set = {1, 3, 4, 5, 6}
print(my_set) # {1, 3, 4, 5, 6}

my_set.discard(4)
print(my_set) # {1, 3, 5, 6}

my_set.remove(6)
print(my_set) # {1, 3, 5}

my_set.discard(2)
print(my_set) # {1, 3, 5}

with pop()

The pop() function remove and return an element.

my_set = set("TutorialReference")

print(my_set.pop()) # print a random element

my_set.pop()
print(my_set)

with clear()

The clear() function remove all the elements from a set.

my_set = set("TutorialReference")

my_set.clear()
print(my_set) # set()

Python Set Operations

Sets can be used to perform mathematical operations on sets, such as union, intersection, difference, and symmetrical difference. We can do this with operators or methods.

To explain these operations, consider the following two sets:

A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

Set Union

The union of two sets is a set of all the elements of both sets.

Union is performed using | operator or using the union() method.

A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

print(A | B)      # {1, 2, 3, 4, 5, 6, 7, 8}

print(A.union(B)) # {1, 2, 3, 4, 5, 6, 7, 8}
print(B.union(A)) # {1, 2, 3, 4, 5, 6, 7, 8}

Set Intersection

The intersection of two sets is a set of elements common to both sets.

Intersection is performed using & operator or using the intersection() method.

A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

print(A & B)      # {4, 5}

print(A.intersection(B)) # {4, 5}
print(B.intersection(A)) # {4, 5}

Set Difference

Difference of the set B from set A (i.e A - B) is a set of elements that are only in A but not in B. Similarly, B - A is a set of elements in B but not in A.

Difference is performed using - operator or using the difference() method.

A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

print(A - B)      # {1, 2, 3}

print(A.difference(B)) # {1, 2, 3}

Set Symmetric Difference

The symmetric difference of two sets is a set of elements present in the first and second set but not in both (excluding the intersection).

Symmetric difference is performed using ^ operator or using the symmetric_difference() method.

A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

print(A ^ B) # {1, 2, 3, 6, 7, 8}

A.symmetric_difference(B) # {1, 2, 3, 6, 7, 8}

B.symmetric_difference(A) # {1, 2, 3, 6, 7, 8}

Set subset

Set A is a subset of set B if all the elements of A are also elements of B. Then set B is a subset of set A.

Subset check is performed using <= operator or using the issubset() method.

A = {1, 2, 3, 4, 5}
B = {1, 2, 3}

print(A <= B)  # False
print(B <= A)  # True
print(A <= A)  # True

print(A.issubset(B))  # False
print(B.issubset(A))  # True
print(A.issubset(A))  # True

Other Common Set Operations

Set Membership Test

The keyword in is used to check whether an item exists in a set.

my_set = set('Tutorial')
 
print('t' in my_set) # True
print('e' in my_set) # False
print('c' not in my_set) # True

Iterating Through a Set

Using a for loop, it is possible to iterate each element of a set.

for letter in set('Tutorial'):
    print(letter)

Output

T
l
t
a
o
i
u
r

Built-in Functions with Set

Built-in functions such as all(), any(), enumerate(), len(), max(), min(), sorted(), sum() etc. are commonly used with sets to perform various tasks.

Function Description
all() Returns True if all elements of the set are true (or if the set is empty).
any() Returns True if any element of the set is true. If the set is empty, returns False.
enumerate() Returns an enumerate object. It contains the index and value for all the items of the set as a pair.
len() Returns the length (the number of items) in the set.
max() Returns the largest item in the set.
min() Returns the smallest item in the set.
sorted() Returns a new sorted list from elements in the set(does not sort the set itself).
sum() Returns the sum of all elements in the set.

Other Python Set Methods

There are many methods of sets (some of which have been explained above).

The following is a list of all the methods available with set objects:

Method Description
add() Adds an element to the set
clear() Removes all elements from the set
copy() Returns a copy of the set
difference() Returns the difference of two or more sets as a new set
difference_update() Removes all elements of another set from this set
discard() Removes an element from the set if it is a member. (Do nothing if the element is not in set)
intersection() Returns the intersection of two sets as a new set
intersection_update() Updates the set with the intersection of itself and another
isdisjoint() Returns True if two sets have a null intersection
issubset() Returns True if another set contains this set
issuperset() Returns True if this set contains another set
pop() Removes and returns an arbitrary set element. Raises KeyError if the set is empty
remove() Removes an element from the set. If the element is not a member, raises a KeyError
symmetric_difference() Returns the symmetric difference of two sets as a new set
symmetric_difference_update() Updates a set with the symmetric difference of itself and another
union() Returns the union of sets in a new set
update() Updates the set with the union of itself and others

Python Frozenset

Frozenset is a new class that has the characteristics of a set, but its elements can't be changed once assigned. While tuples are immutable lists, frozensets are immutable sets.

  • Sets, being mutable, are not hashable, so they can't be used as keys to a dictionary.
  • Frozensets are hashable and can be used as keys to a dictionary.

Frozensets can be created using the frozenset() function.

This data type supports methods like copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and union().

A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])

print(A.isdisjoint(B)) # frozenset({1, 2})
print(A | B) # frozenset({1, 2, 3, 4, 5, 6})
Navigation