Python Sets (With Examples)

Learn to create and use sets in Python 3 with examples.

Python Sets (With Examples)

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.

Topics Covered

  1. How to create a set?
  2. Mathematical operations with sets
  3. Using methods with sets
  4. Problems to solve

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 Set difference

In mathematical set theory, the difference between 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

Union of 2 sets

The union of two sets A and B is the set of elements that 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

Intersection of 2 sets

The intersection of two sets A and B are the set of elements that 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

Symmetric difference of 2 sets

The symmetric difference of two sets A and B are the set of elements that 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'}

Duplication is 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()

Problems

  1. Write a Python program to check if a set contains one or more items that are False.
    Hint - Use any() method
  2. Write a Python program to return all the unique elements of a list.
  3. Write a Python program to return the common elements between 2 sets.
  4. Write a Python program to check if there are any common elements between any 2 lists.
    Hint - Use the intersection concept of sets.

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe