If you’re working with sets in Python, you may come across the term “union”. Sets are unordered collections of unique elements, and they can be useful in a variety of applications, such as data analysis and web development. In this article, we’ll dive into what a set union is, how it works, and how to use it in your Python code.
What is a Set Union?
A set union is a mathematical operation that combines two sets into a single set that contains all the unique elements from both sets. It’s denoted by the symbol ∪.
In Python, you can use the union()
method or the |
operator to perform a set union. Both return a new set that contains all the unique elements from the input sets.
How to Use the union()
Method
The union()
method can be called on any set object in Python. Here’s the syntax:
set1.union(set2)
This will return a new set that contains all the unique elements from set1
and set2
.
Let’s take a look at some examples to see how this works.
Example 1: Basic Set Union
set1 = {1, 2, 3}
set2 = {2, 3, 4}
union_set = set1.union(set2)
print(union_set)
Output:
{1, 2, 3, 4}
In this example, we have two sets (set1
and set2
) that have some overlapping elements. When we call set1.union(set2)
, we get a new set that contains all the unique elements from both sets.
Example 2: Union with Duplicate Elements
set1 = {1, 2, 3}
set2 = {2, 3, 3, 4}
union_set = set1.union(set2)
print(union_set)
Output:
{1, 2, 3, 4}
In this example, set2
has a duplicate element (3). However, the union()
method only includes unique elements in the resulting set.
Example 3: Union with Strings
set1 = {"apple", "banana", "cherry"}
set2 = {"banana", "cherry", "orange"}
union_set = set1.union(set2)
print(union_set)
Output:
{'banana', 'cherry', 'apple', 'orange'}
The union()
method works the same way with sets of strings as it does with sets of integers.
Example 4: Union with Empty Sets
set1 = {1, 2, 3}
set2 = set()
union_set = set1.union(set2)
print(union_set)
Output:
{1, 2, 3}
If one of the sets is empty, the union()
method will simply return the non-empty set.
Example 5: Union with Multiple Sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = {3, 4, 5}
union_set = set1.union(set2, set3)
print(union_set)
Output:
{1, 2, 3, 4, 5}
You can also pass multiple sets to the union()
method. It will combine all the unique elements from all the sets into a single set.
Example 6: Union using the |
Operator
set1 = {1, 2, 3}
set2 = {2, 3, 4}
union_set = set1 | set2
print(union_set)
Output:
{1, 2, 3, 4}
As an alternative to the union()
method, you can use the |
operator to perform a set union. In this example, set1 | set2
returns a new set containing all the unique elements from both set1
and set2
.
Conclusion
In this article, we’ve covered what a set union is and how to use the union()
method and the |
operator in Python. By using these techniques, you can easily combine two or more sets into a single set that contains all the unique elements. This can be useful in a variety of applications, from data analysis to web development.