Python Sets (With Examples)

By Lenin Mishra

Sets are an unordered collection of unique and immutable objects. It supports mathematical operations from the set theory.

An item appears only once in a set, no matter how many times it is added.

How to create a set?

You can use the set() construct with iterables or use curly braces {} to create a set in Python.

Code

# EMpty set
x = set()
print(x)

# Using iterables
x = set("Pylenin")
print(x)

x = set([1, 2, 3, 4])
print(x)

# Using curly braces
x = {1, 2, 3, 4}
print(x)

Output

set()
{'l', 'y', 'e', 'P', 'i', 'n'}
{1, 2, 3, 4}
{1, 2, 3, 4}

You cannot declare a set by using empty curly braces. Python will assume its a dictionary.

Mathematical operations with sets

Set membership test

You can use the in operator to check if an element exists in a set.

Code

x = {1, 2, 3, 4}

if 1 in x:
    print("Element exists")
else:
    print("Element doesn't exist")

Output

Element exists

Difference between 2 sets

Python Sets - Difference

In mathematical set theory, the difference of two sets(A & B) is A - B. It is the set of all elements of A that are not elements of B. You can perform the same operation in Python

Code

x = {1, 2, 3, 4}
y = {2, 3, 4, 5}

print(x - y)
print(y - x)

Output

{1}
{5}

Union of 2 sets

Python Sets - Union

The union of two sets A and B is the set of elements which are in A, B or in both A and B.

Code

x = {1, 2, 3, 4}
y = {2, 3, 4, 5}

print(x | y)

Output

{1, 2, 3, 4, 5}

Intersection of 2 sets

Python Sets - Intersection

The intersection of two sets A and B are the set of elements which are in both A and B.

Code

x = {1, 2, 3, 4}
y = {2, 3, 4, 5}

print(x & y)

Output

{2, 3, 4}

Symmetric difference of sets

Python Sets - Symmetric Difference

The symmetric difference of two sets A and B are the set of elements which are in A and B, but not common to A and B.

Code

x = {1, 2, 3, 4}
y = {2, 3, 4, 5}

print(x ^ y)

Output

{1, 5}

Using methods with sets

Adding elements to sets

To add elements to a set, you can use the add() method.

Code

x = {1, 2, 3, 4}

x.add(10)
print(x)

Output

{1, 2, 3, 4, 10}

With add() method, you can add only a single element. To add multiple elements, use the update() method.

Adding multiple elements to sets with update()

Code

x = {1, 2, 3, 4}

x.update([4, 5, 6, 7])
print(x)

x.update(["Pylenin", "Python"], {"greeting"})
print(x)

Output

{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5, 6, 7, 'Python', 'Pylenin', 'greeting'}

Duplications are always avoided with sets.

Remove an element from a set

To remove an element from a set, use remove() method.

Code

x = {1, 2, 3, 4}

x.remove(2)
print(x)

Output

{1, 3, 4}

If the element you are trying to remove doesn’t exist, Python will throw a KeyError.

To avoid getting such errors, use the discard() method with sets.

Code/Output

x = {1, 2, 3, 4}

x.discard(20)
print(x)
>>> {1, 2, 3, 4}

x.remove(20)
print(x)
>>> KeyError: 20

Remove multiple elements from a set

To remove multiple elements from a set, use the clear() method.

Code

x = {1, 2, 3, 4}

x.clear()
print(x)

Output

set()