Python Set Operations and More: All You Need to Know About Python Sets

Smiling young couple enjoying morning coffee with computer, sitting on sofa at home together; happy man and woman laughing looking on laptop screen having fun while programming

Even if you’re just starting your Python journey, you’ve most likely come across Python sets. But do you know how to use them effectively? This article aims to cover all the main Python set operations to give you a better feel for when and how to use this important data structure.

What Are Python Sets?

First, let’s start with the basics. A set is a built-in type in Python that has a number of important characteristics:

  • Sets are unordered. In other words, the items are inserted in random order, so you can’t access elements using indices.
  • Sets include only unique elements. No duplicates are allowed in sets.
  • A set is mutable. A set can be modified, and it’s possible to add and remove items from one.
  • Elements of a set must be of an immutable type. For example, strings, integers, floats, and tuples are acceptable types for a set.
  • A set may include elements of different types. For example, you can include a mix of numbers, strings, and tuples in one set.

Now it’s time to see how to define and modify Python sets.

Creating and Changing a Set in Python

Creating sets

There are two ways to create a set: you can use the built-in set() function or, alternatively, define a set with curly braces. Here are some examples:

# Creating sets using built-in function
set_one = set((0, "one", (2, 3, 4)))
set_one
{(2, 3, 4), 0, 'one'}

# Creating sets using built-in function
set_two = set("Game of Thrones")
set_two
{' ', 'G', 'T', 'a', 'e', 'f', 'h', 'm', 'n', 'o', 'r', 's'}

# Creating sets using curly braces
set_three = {0, "one", (2, 3, 4)}
set_three
{(2, 3, 4), 0, 'one'}

# Creating sets using curly braces
set_four = {"Game of Thrones"}
set_four
{'Game of Thrones'}

The argument to the set() function needs to be an iterable that generates a list of objects (e.g., tuples, lists, strings); these will be inserted into the set. On the other hand, if you use curly braces, the objects themselves are placed manually. So you’ve probably spotted the difference in applying these two approaches:

  • We pass in a tuple to the set() function to create set_one, but we place the objects directly into the curly braces to get set_three with the same elements.
  • The set() function treats the string that we pass in to create set_two as an iterable, and so the resulting set is a collection of letters from this string, while curly braces treat a string as a distinct element of the set and so we get set_four with this string as the only member.

Also, note that the resulting sets are unordered, and duplicate values are only represented in the set once (as with the letter ‘e’ in the second example).

Checking set size and membership

You can use the len() function to check the number of elements in a set, and you can also check if a particular element exists or doesn’t exist in a set using the in or not in operators, respectively.

# Checking the number of elements in a set
len(set_one)
3

# Checking if an element is in a set
0 in set_one
True

# Checking if an element is not in a set
0 not in set_one
False

Adding elements to a set

You can use the add() method to insert a single element into a set. If you’d like to insert multiple objects into a set at once, use the update() method. Here are some examples:

# Adding a single element using add() method
my_set = {'a', 'b', 'c'}
my_set.add('d')
my_set
{'a', 'b', 'c', 'd'}

# Adding multiple elements using update() method
my_set.update('e', 'f', 'g', 'b')
my_set
{'a', 'b', 'c', 'd', 'e', 'f', 'g'}

# Adding multiple elements of different types (i.e., adding a tuple and another set)
my_set.update(('a', 'h'), {'c', 'i', 'j'})
my_set
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}

Removing elements from a set

There are a number of ways to remove items from a set:

  • Use the remove() method if you want to remove a single element from a set, or get an error message if the respective item is not in the set.
  • Use the discard() method if you want to remove a single element from a set but don’t need an error message if the given item is not in the set.
  • Use the pop() method to remove and return a random element from a set.
  • Use the clear() method to remove all items from a set.

We’ll see later how you can remove multiple elements from a set without using a loop.

# Using remove() to remove an item that doesn't exist in a set
my_set.remove('o')
my_set
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
 in ()
      1 # Using remove() to remove an item that doesn't exist in a set
----> 2 my_set.remove('o')
      3 my_set

KeyError: 'o'

# Using discard() to remove an item that doesn't exist in a set
my_set.discard('o')
my_set
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}

# Using pop() to remove and return a random element
print(my_set.pop())
print(my_set)
j
{'d', 'b', 'e', 'c', 'a', 'i', 'h', 'g', 'f'}

# Using clear() to remove all elements from a set
my_set.clear()
my_set
set()

These are the basic methods you can use to modify a set. Let’s now move on to some more advanced Python set operations and methods.

Python Set Operations and Methods

We can use Python sets to carry out mathematical set operations like union, intersection, difference, and symmetric difference. These operations can be performed using either operators or methods.

However, there is one important difference between these two approaches: operators can work only with sets, while methods accept any iterable (e.g., lists, tuples) as an argument, convert it to a set, and then perform the operation.

Don’t worry if this is not clear to you yet—we’ll look at some examples below.

Set Union

If there are two sets, first_set and second_set, the union of these two sets is the set of all elements from both sets. You can get a union of two sets using the union() method or the | operator. However, if you want to get a union of a set and a list, the | operator is not going to work, and you will need to use the union() method.

# Initializing sets
first_set = {1, 3, 5, 7, 9}
second_set = {2, 4, 6, 8, 10}
# Set union using | operator
first_set | second_set
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

# Set union using union() method
first_set.union(second_set)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

# Getting a union of a set and a list
first_set.union([2, 4, 6, 8, 10])
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Set Intersection

The intersection of two sets, first_set and second_set, is the set of all elements common to both sets. This operation can be performed using the & operator or the intersection() method.

# Initializing sets
first_set = {1, 2, 3, 4, 5}
second_set = {4, 5, 6, 7, 8}
third_set = {4, 5, 9, 10}
# Performing intersection using & operator
first_set & second_set & third_set
{4, 5}

# Performing inteesection using intersection() method
first_set.intersection(second_set, third_set)
{4, 5}

As you can see, both the intersection() method and the & operator allow you to create an intersection for more than two sets. Note that this applies not only to set intersections but also to other operations.

Set Difference

The difference of first_set and second_set is the set of all elements that are only in first_set and not in second_set. You can create the difference of two sets using either the – operator or the difference() method.

# Initializing sets
first_set = {1, 2, 3, 4, 5}
second_set = {4, 5, 6, 7, 8}
# Performing difference using - operator
print(first_set - second_set)
print(second_set - first_set)
{1, 2, 3}
{8, 6, 7}

# Performing difference using difference() method
print(first_set.difference(second_set))
print(second_set.difference(first_set))
{1, 2, 3}
{8, 6, 7}

As you can see from the examples, the order of the sets matters when performing the set difference operation.

Set Symmetric Difference

The symmetric difference operation allows you to get the elements that are either in first_set or in second_set but not in both. Again, you have two options for performing this operation: the symmetric_difference() method or the ^ operator.

# Initializing sets
first_set = {1, 2, 3, 4, 5}
second_set = {4, 5, 6, 7, 8}
# Performing symmetric difference using ^ operator
first_set^second_set
{1, 2, 3, 6, 7, 8}

# Performing symmetric difference using symmetric_difference() method
first_set.symmetric_difference(second_set)
{1, 2, 3, 6, 7, 8}

Update operations

Each of the operations we discussed above (union, intersection, difference, and symmetric difference) can be also used to update a set. We just need to use an augmented assignment form for the operators (|=, &=, -=, ^=), or the corresponding methods update(), intersection_update(), difference_update(), and symmetric_difference_update()). Here are some examples:

# Initializing sets
first_set = {1, 2, 3, 4, 5}
second_set = {4, 5, 6, 7, 8}
# Modifying a set by union using update() method
first_set.update(second_set)
first_set
{1, 2, 3, 4, 5, 6, 7, 8}

# Modifying a set by intersection using an operator
first_set &= second_set
first_set
{4, 5, 6, 7, 8}

# Modifying a set by difference using difference_update() method
first_set.difference_update((6, 7, 8))
first_set
{4, 5}

# Modifying a set by symmetric difference using an operator
first_set ^= second_set
first_set
{6, 7, 8}

Notice how we were able to remove multiple elements from first_set by specifying the elements we wanted to remove in a tuple that we passed in as an argument to the difference_update() method.

Other set methods

Let’s briefly review some methods that will help you to determine how sets relate to each other:

  • isdisjoint() returns true if two sets have any elements in common.
  • issubset() or the <= operator returns true if the first set is a subset of the second one.
  • issuperset() or the >= operator returns true if the first set contains every element of the second one.

Python frozenset

If you’ve ever attempted to use set elements as dictionary keys, you know that this doesn’t work because sets are mutable and thus unhashable. Luckily, Python has another built-in type named frozenset that has all the properties of a set but is immutable. Frozensets are hashable and are accepted as keys to a dictionary. Check out the Python Basics. Part 2 course for more info on creating dictionaries.

You can create a frozenset using the frozenset() function. Being immutable, a frozenset doesn’t have methods for adding or removing elements. However, update operations do work with frozensets. That’s because these operations do not modify the original set but rather assign the set to a new object.

Wrap up

Now you know how to define a set object in Python, how to modify a set, and what kinds of operations can be performed with sets. You also know the main characteristics of Python sets and should have a better feeling for when a set is an appropriate choice.

All you need now is some practice! Check out the Python Basics. Part 3 course for a number of interactive exercises that cover sets, tuples, and date/time objects.

In case you also need a refresher on lists, dictionaries, and text files, Python Basics. Part 2 will provide you with easy-to-understand explanations and lots of hands-on exercises.

Kateryna Koidan

Kateryna is a data science writer from Kyiv, Ukraine. She worked for BNP Paribas, the leading European banking group, as an internal auditor for more than 6 years. More recently, she decided to pursue only the favorite part of her job—data analysis. Now she is continuing her self-education with deep-learning courses, enjoys coding for data analysis and visualization projects, and writes on the topics of data science and artificial intelligence. Kateryna is also a proud mother of two lovely toddlers, who make her life full of fun.

comments powered by Disqus