This tutorial will go through how to get the union of sets in Python with the help of some code examples.
Table of contents
What is a Set?
A Python set is one of the four built-in data types in Python to store collections of data. A set is a collection that has no duplicate elements, is unordered, unchangeable and not indexed. We can use sets for membership testing in a collection of items and removing duplicates. For further reading on the use of sets for removing duplicates, go to the article How to Get Unique Values from List in Python. Set objects also support mathematical operations like union, intersection, difference and symmetric difference. Union and intersection are the components of Jaccard similarity, which is a ubiquitous similarity measure in statistics.
What is Set Union?
The union of two sets is the set that contains all the elements of both sets. We can find the union between sets in Python using the union() method:
set_1.union(set_2, set_3, ..., set_n)
We can pass any number of sets the union() method. The method returns a set with all elements common to the sets. If we do not pass a parameter to union(), it returns a copy of the set.
Using the Union Method
Let’s look at an example of using the union method with three sets. We will find the union between all possible set pairs and then the union between all three sets.
set_x = {3, 4, 9, 12}
set_y = {6, 5, 14, 9}
set_z = {1, 11, 6, 2, 8}
# Union between two sets
x_union_y = set_x.union(set_y)
y_union_z = set_y.union(set_z)
x_union_z = set_x.union(set_z)
# Union between all three sets
x_y_z = set_x.union(set_y, set_z)
print('set_x U set_y: ', x_union_y)
print('set_y U set_z: ', y_union_z)
print('set_x U set_z: ', x_union_z)
print('set_x U set_y U set_z: ', x_y_z)
Let’s run the code to get the result:
set_x U set_y: {3, 4, 5, 6, 9, 12, 14}
set_y U set_z: {1, 2, 5, 6, 8, 9, 11, 14}
set_x U set_z: {1, 2, 3, 4, 6, 8, 9, 11, 12}
set_x U set_y U set_z: {1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 14}
Using the OR Operator |
We can also use the OR operator | to get the union of sets. Let’s look at an example of using the OR operator with three sets. We will find the union between all possible set pairs and then the union between all three sets.
set_x = {3, 4, 9, 12}
set_y = {6, 5, 14, 9}
set_z = {1, 11, 6, 2, 8}
# Union between two sets
x_union_y = set_x | set_y
y_union_z = set_y | set_z
x_union_z = set_x | set_z
# Union between all three sets
x_y_z = set_x | set_y | set_z
print('set_x U set_y: ', x_union_y)
print('set_y U set_z: ', y_union_z)
print('set_x U set_z: ', x_union_z)
print('set_x U set_y U set_z: ', x_y_z)
Let’s run the code to get the result:
set_x U set_y: {3, 4, 5, 6, 9, 12, 14}
set_y U set_z: {1, 2, 5, 6, 8, 9, 11, 14}
set_x U set_z: {1, 2, 3, 4, 6, 8, 9, 11, 12}
set_x U set_y U set_z: {1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 14}
Summary
Congratulations on reading to the end of this tutorial. You can get the union of sets in Python using the intersection method or the | operator. You can get the union of any number of sets using both methods.
For further reading on set methods, go to the article: How to Add to a Set in Python.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.