Unite and Conquer: Exploring Union of Sets in Python
You must have learned the concept of sets in your mathematics and there, you must've learned the Union operation. This is exactly the same but you will be learning how to implement it in Python.
I already mentioned the concept of Union in sets in Python briefly in our previous article, Sets in Python. Now that you know what sets in Python, let us dive through each and every concept starting with Union so without further ado let's get started.
Union of Sets in Python:
You learned about sets in Python and in that, I briefly explained the concept of the superset in Python. We also discussed operations like intersection of sets, difference of sets, symmetric difference of sets, subset, superset and disjoint sets in Python in detail whereas this article focuses on the next operation of the set which is union.
In Python, the union operation on sets is used to combine two or more sets into a new set. The new set will contain all of the elements from the original sets but with no duplicates. You must've learned in-depth about the creation of sets in the article Set Comprehension. This article will be easy if you've already gone through it as this is the next step after creating sets.
The union operation is one of the most commonly used set operations. It can be used to combine sets of data to create a larger set, or to find all of the unique elements in a collection of sets.
The union of sets in Python can be performed using the union() method or the pipe (|) operator.
- Using the union() method: The union() method takes one or more sets as arguments and returns a new set that contains all of the elements from the arguments. For example, the following code creates two sets and then uses the union() method to create a new set that contains all of the elements from both sets:
>>> set1 = {1, 2, 3}
>>> set2 = {4, 5, 6}
>>> new_set = set1.union(set2)
>>> print(new_set)
{1, 2, 3, 4, 5, 6}
- Using the pipe (|) operator: The pipe operator (|) can also be used to perform the union operation on sets. For example, the following code is equivalent to the previous code:
>>> new_set = set1 | set2
>>> print(new_set)
Example Programs for Union of Sets in Python:
Let us see simple programs to understand the process of Union of Sets much better.
Sample Program 1:
Create a program to perform the Union function to a set of numbers:
>>> def find_union(set1, set2):
>>> new_set = set1 | set2
>>> return new_set
>>> set1 = {1, 2, 3, 4, 5}
>>> set2 = {6, 7, 8, 9, 10}
>>> new_set = find_union(set1, set2)
>>> print(new_set)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Sample Program 2:
Create a program to perform the Union function to a set containing different data types:
>>> def find_union(set1, set2):
>>> new_set = set1 | set2
>>> return new_set
>>> set1 = {1, "a", "b", 3.14}
>>> set2 = {2, "c", "d", 5}
>>> new_set = find_union(set1, set2)
>>> print(new_set)
{1, 2, 3.14, 'a', 'b', 'c', 'd', 5}
As you can see, in both these programs the union() function has been used to perform the Union operation in the given sets. This is how you can apply Union of Sets in Python.
Conclusion:
Let us summarise whatever you learned up till now, the union of sets in Python is a powerful tool that can be used to combine data from different sets. The union operation can be performed using the union() method or the pipe (|) operator. The union() method takes one or more sets as arguments and returns a new set that contains all of the elements from the arguments. The pipe (|) operator can also be used to perform the union operation on sets. The pipe operator is a shortcut for the union() method.
I hope this article has been helpful in explaining the concept of the Union of Sets in Python and I will see you in the next article with an amazing concept. Until then, if you have any doubts, feel free to contact us.