In some of my previous articles we were talking about the basics of sets and set methods. If you’re interested, feel free to read them. Today we’ll be talking about set operations. There are several set operations we can use: difference, union, intersection and some more. They are pretty much like the operations on sets in mathematics.
You can also watch the video version of this article:
Table of Contents
Difference of Sets
The method difference returns a new set with the difference of the sets involved. The difference of 2 sets includes all the elements that belong to one set and not to the other, or, more simply, all the elements of the two sets minus those which belong to both. Let’s define two sets:
>>> days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
>>> weekend = {"Saturday", "Sunday"}
The difference of the two sets contains all the elements of days which are not found in weekend:
>>> weekdays = days.difference(weekend)
>>> weekdays
{'Tuesday', 'Thursday', 'Friday', 'Monday', 'Wednesday'}
There is an even shorter syntax to find the difference of two sets. We can just use the minus operator:
>>> days - weekend
{'Tuesday', 'Thursday', 'Friday', 'Monday', 'Wednesday'}
We can find the difference of more than two sets:
>>> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> odd_numbers = {1, 3, 5, 7, 9}
>>> prime_numbers = {2, 3, 5, 7}
Let’s find all the numbers that are in numbers but not in odd_numbers. So we get {2, 4, 6, 8}.
Then let’s find those of the above numbers which are not in prime_numbers. So we get only even composite (= not prime) numbers:
>>> even_composite_numbers = numbers.difference(odd_numbers).difference(prime_numbers)
>>> even_composite_numbers
{8, 4, 6}
We can also use the shortcut here:
>>> numbers - odd_numbers - prime_numbers
{8, 4, 6}
The difference_update Method
A similar method is difference_update. It works like difference plus it removes all the elements of another set from the set it’s called on:
>>> elements = {"iron", "carbon", "lead", "sulfur", "copper"}
>>> nonmetals = {"carbon", "sulfur"}
>>> elements.difference_update(nonmetals)
>>> elements
{'copper', 'iron', 'lead'}
Here we have a shortcut as well. Instead of using the method x.difference_update(y)we can use the minus operator x = x – y.
>>> elements = {"iron", "carbon", "lead", "sulfur", "copper"}
>>> nonmetals = {"carbon", "sulfur"}
>>> elements = elements - nonmetals
>>> elements
{'iron', 'lead', 'copper'}
or even shorter, the -= augmented operator:
>>> elements = {"iron", "carbon", "lead", "sulfur", "copper"}
>>> nonmetals = {"carbon", "sulfur"}
>>> elements -= nonmetals
>>> elements
{'copper', 'iron', 'lead'}
The symmetric_difference Method
Another method is symmetric_difference. It returns a new set with the elements in either of the two sets but not in both:
>>> prime_numbers = {2, 3, 5, 7, 11, 13}
>>> numbers_above_10 = {11, 12, 13, 16, 19}
>>> prime_numbers.symmetric_difference(numbers_above_10)
{16, 2, 19, 3, 5, 7, 12}
The shortcut for this method is ^, So x ^ y works like x.symmetric_difference(y):
>>> prime_numbers ^ numbers_above_10
{16, 2, 19, 3, 5, 7, 12}
Union of Sets
The method union is used to return a union of two sets as a new set. The union contains all elements that are in either set:
>>> big_mammals = {"elephant", "reindeer", "rhino", "whale"}
>>> african_mammals = {"hippo", "elephant", "giraffe", "lion"}
>>> mammals = big_mammals.union(african_mammals)
>>> mammals
{'rhino', 'reindeer', 'hippo', 'giraffe', 'elephant', 'lion', 'whale'}
A shortcut for this method also exists. It’s the pipe operator |. So x | y works like x.union(y):
>>> big_mammals | african_mammals
{'rhino', 'reindeer', 'hippo', 'giraffe', 'elephant', 'lion', 'whale'}
Intersection of Sets
Another method is intersection. It returns the intersection of two sets as a new set. The intersection contains all elements that belong to both of the sets:
>>> odd_numbers = {1, 3, 5, 7, 9, 11, 13, 15}
>>> prime_numbers = {2, 3, 5, 7, 11, 13}
>>> odd_prime_numbers = odd_numbers.intersection(prime_numbers)
>>> odd_prime_numbers
{3, 5, 7, 11, 13}
This method has a shortcut as well. It’s the ampersand operator &. So x & y works like x.intersection(y):
>>> odd_numbers & prime_numbers
{3, 5, 7, 11, 13}