In Post #77 and Post #78, we learned the four main set operations: union, intersection, difference, and symmetric difference. While the code for these operations is straightforward, the concepts themselves can sometimes feel a bit abstract.
In this post, we’ll take a step back from the code and use a simple visual tool to build a strong mental model for these operations: the Venn diagram.
What is a Venn Diagram?
A Venn diagram is a simple way to show the relationship between sets. For our purposes, we can imagine two overlapping circles inside a box.
- One circle represents all the items in Set A.
- The other circle represents all the items in Set B.
- The overlapping section in the middle represents the items that are present in both Set A and Set B.
This simple picture gives us three distinct areas: items only in A, items only in B, and items in both.
Visualizing the Operations
We can now map each of the four set operations to a specific area of this diagram.
Union (A | B)
The union is everything in both sets combined. In a Venn diagram, this corresponds to the entire area of both circles, including their overlap. If an item is in A, or in B, or in both, it’s part of the union.
Intersection (A & B)
The intersection is only the elements that are common to both sets. This corresponds to just the overlapping area in the middle of the Venn diagram. It’s the part that both sets share.
Difference (A – B)
The difference is the elements that are in A but not in B. This corresponds to the part of circle A that does not overlap with circle B. It’s the “A-only” section. Remember that A - B
is different from B - A
, which would be the “B-only” section.
Symmetric Difference (A ^ B)
The symmetric difference is the elements that are in either A or B, but not in both. This corresponds to the two outer sections of the circles—the “A-only” part and the “B-only” part combined—but excludes the overlapping intersection.
Tying it Back to Our Code
Let’s recall our artist sets from the previous posts.
local_artists = {"Taylor Swift", "Ed Sheeran", "Dua Lipa", "Harry Styles"}
global_artists = {"Ed Sheeran", "The Weeknd", "Harry Styles", "Adele"}
Now let’s map them to a Venn diagram:
- The Intersection (
&
): The overlapping area contains the common artists:{"Ed Sheeran", "Harry Styles"}
. - The Difference (
local_artists - global_artists
): The “local artists only” section contains:{"Taylor Swift", "Dua Lipa"}
. - The Difference (
global_artists - local_artists
): The “global artists only” section contains:{"The Weeknd", "Adele"}
. - The Symmetric Difference (
^
): The two non-overlapping sections together contain:{"Taylor Swift", "Dua Lipa", "The Weeknd", "Adele"}
. - The Union (
|
): All three sections combined contain all six unique artists.
What’s Next?
By visualizing set operations with Venn diagrams, the meaning of union, intersection, difference, and symmetric difference becomes much more intuitive. Keep this mental picture in mind whenever you are working with sets, as it can help you quickly determine which operation you need to solve your problem.
While these mathematical operations are powerful, by far the most common, everyday use for a set is a much simpler task. In Post #80, we will look at the number one reason you’ll reach for a set in your day-to-day Python programming: finding the unique items in a list.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com