Skip to main content

Python Set

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.

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

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

# set can't have mutable items
# here [10, 20] is a mutable list
# and this will cause an error.
# my_set = {1, 2, [10, 20]}
# TypeError: unhashable type: 'list'

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

# set can't have duplicates
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

# 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.

# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a
print(type(a)) # <class 'dict'>

# initialize a with set()
a = set()

# check data type of a
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.

note

The update() method can take tuples, lists, strings or other sets as arguments. In all cases, duplicates are avoided.

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

# uncomment to get an error
# my_set[0]
# TypeError: 'set' object does not support indexing

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

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

# add list and set
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.
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set) # {1, 3, 4, 5, 6}

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

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

# discard an element not present in my_set
my_set.discard(2)
print(my_set) # {1, 3, 5}

# remove an element not present in my_set
# my_set.remove(2)
# You get a KeyError

with pop()

The pop() function remove and return an element.

note

Since the set is an unordered data type, there is no way to determine which element will be extracted. It is completely arbitrary.

# initialize my_set
my_set = set("TutorialReference")

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

# pop another element
my_set.pop()
print(my_set)

with clear()

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

# initialize my_set
my_set = set("TutorialReference")

# clear my_set
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.

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

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

# use union() function
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.

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

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

# use union() function
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.

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

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

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

A - B is not equals to B - A

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

# use - operator on B
print(B - A) # {8, 6, 7}

# use difference function on B
print(B.difference(A)) # {8, 6, 7}

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.

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

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

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

# use symmetric_difference function on B
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}

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

# use issubset() function
print(A.issubset(B)) # False
print(B.issubset(A)) # True
print(A.issubset(A)) # True
note

The proper subset operator < checks if set A is a proper subset of set B

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

# use < operator
print(A < B) # False
print(A < A) # False

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
note

See Python For Loop to learn more.

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.

FunctionDescription
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:

MethodDescription
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
note

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().

note

Frozensets are immutable, so they have no methods for adding or removing items.

# initialize A and B
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})

# uncomment to get an Error
# print(A.add(3))
# AttributeError: 'frozenset' object has no attribute 'add'