Python Set Operations: Complete Guide

This article was first published on PyShark , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

In this article we will focus on a complete walk through of Python set operations.




Table of contents


Introduction

At this point the reader should be familiar with Python sets. If you would like a refresher, or are new to sets, please check out the Python sets beginner’s guide.

In this tutorial, let’s think of sets from a mathematical point of view and consider parts of set theory. A set is simply a collection of distinct elements (or mathematical objects).

For the purposes of this tutorial, we will work with sets that are collections of numbers. Something like: A = {1, 2, 3, 5, 7} and B = {1, 2, 4, 8, 9}.


Python set union

In set theory, a set union is a set of collection of sets. What this means is that if we have two sets, A and B, we can combine them and only take distinct elements (meaning that repeated elements will be dropped).

But how does this look mathematically?

Here it is:

$$A \cup B = \{x: x \in A \textit{ or x} \in B\}$$

A numerical example can be useful to understand it. Consider two sets:

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

Or visually:

Set Defined Example

In this case:

$$A \cup B = \{1, 2, 3, 5, 7, 4, 8, 9\}$$

Elements 1 and 2 are present in both sets, A and B. When unioning two sets, we only consider each repeated element once.

The yellow area in the infographic below shows the union of set A with set B:

Set Union in Python

Now let’s see how we can do the same operation using Python. We will first create both sets, A and B. And then use the .union() method of Python set to perform the union operation:

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

set_union = A.union(B)

print(set_union)

And you should get:

{1, 2, 3, 4, 5, 7, 8, 9}

Which is exactly the same set, with same elements in the yellow area in the infographic above.





Python set intersection

In set theory, a set intersection is a set of collection of mutual elements of sets. What this means is that if we have two sets, A and B, a set intersection will contain all elements that are present in both sets.

Mathematically speaking:

$$A \cap B = \{x: x \in A \textit{ and x} \in B\}$$

A numerical example can be useful to understand it. Consider two sets:

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

Or visually:

Set Defined Example

In this case:

$$A \cap B = \{1, 2\}$$

Elements 1 and 2 are the only present together in both sets, A and B.

The yellow area in the infographic below shows the intersection of set A with set B:

Set Intersection in Python

Now let’s see how we can do the same operation using Python. We will first create both sets, A and B. And then use the .intersection() method of Python set to perform the intersection operation:

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

set_intersection = A.intersection(B)

print(set_intersection)

And you should get:

{1, 2}





Python set difference

In set theory, the set difference between set B and set A, is also referred to as the relative complement of set A in set B. It is defined as the set of elements that are present in set B but not present in set A.

Alternatively, the set difference between set A and set B is the set of elements that are present in set A but not present in set B.

Mathematically:

$$B \setminus A = \{x \in B : x \notin A\}$$

A numerical example can be useful to understand it. Consider two sets:

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

Or visually:

Set Defined Example

In this case:

$$B \setminus A = \{4, 8 ,9\}$$

Elements 1 and 2 are the only present together in both sets, A and B. But elements 4, 8, 9 are only present in set B. Hence the set difference between set B and set A are exactly these elements: {4, 8 ,9}.

The yellow area in the infographic below shows the set difference between set B and set A:

Set Difference in Python

Now let’s see how we can do the same operation using Python. We will first create both sets, A and B. And then use the .difference() method of Python set to perform the difference operation:

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

set_difference = B.difference(A)

print(set_difference)

And you should get:

{8, 9, 4}





Python set symmetric difference

In set theory, the symmetric difference between two sets (A and B) is also referred to as the disjunctive join. It is defined as the set of elements present in either set A or set B, but not in both together (not in the intersection of set A and set B).

Mathematically it is calculated as a union of both differences:

$$A \bigtriangleup B = (A \setminus B) \cup (B \setminus A)$$

A numerical example can be useful to understand it. Consider two sets:

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

Or visually:

Set Defined Example

There are three steps to find the symmetric difference here:

  1. Find set difference between set B and set A: \(A \setminus B = \{3, 7, 5\}\) because these elements are present in set A but not present in set B.
  2. Find set difference between set A and set B: \(B \setminus A = \{4, 8, 9\}\) because these elements are present in set B but not present in set A.
  3. Union two set differences found in Step 1 and Step 2: \((A \setminus B) \cup (B \setminus A) = \{3, 7, 5, 4, 8, 9\}\)

In this case:

$$A \bigtriangleup B = \{3, 7, 5, 4, 8, 9\}$$

The yellow area in the infographic below shows the symmetric difference of set A and set B:

Python Set Symmetric Difference

Now let’s see how we can do the same operation using Python. We will first create both sets, A and B. And then use the .symmetric_difference() method of Python set to perform the symmetric difference operation:

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

set_symmetric_difference = A.symmetric_difference(B)

print(set_symmetric_difference)

And you should get:

{3, 4, 5, 7, 8, 9}





Conclusion

This article is a complete guide on Python set operations with detailed formula explanations and examples.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Data Structures articles.

The post Python Set Operations: Complete Guide appeared first on PyShark.

To leave a comment for the author, please follow the link and comment on their blog: PyShark .

Want to share your content on python-bloggers? click here.