Python – Set Union and Intersection (basic and advanced examples)

Updated: February 12, 2024 By: Guest Contributor Post a comment

Introduction

Sets in Python are collections that are unordered, changeable, and do not allow duplicate values. They are incredibly useful for performing mathematical set operations like union, intersection, difference, and symmetric difference. This article will focus on union and intersection operations, offering examples ranging from basic to advanced use cases.

Basic Examples

To begin with, let’s explore the most straightforward examples of set union and intersection.

Example 1: Union of Two Sets

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)
# Output: {1, 2, 3, 4, 5}

This example demonstrates the basic operation of union, which combines elements from both sets without duplicates.

Example 2: Intersection of Two Sets

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)
# Output: {3}

This basic example of intersection shows how we can find common elements between two sets.

Intermediate Examples

Moving to a more complex scenario, we can perform these operations with more than two sets.

Example 3: Union Across Multiple Sets

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {5, 6, 7}
union_set = set1.union(set2, set3)
print(union_set)
# Output: {1, 2, 3, 4, 5, 6, 7}

This example illustrates a union across three sets, showcasing the flexibility of the union method.

Example 4: Intersection Across Multiple Sets

set1 = {1, 2, 3, 4}
set2 = {2, 3, 4, 5}
set3 = {3, 4, 5, 6}
intersection_set = set1.intersection(set2, set3)
print(intersection_set)
# Output: {3, 4}

Similarly, we can find common elements across multiple sets using the intersection method.

Advanced Examples

For those looking to leverage these operations in more complex applications, consider the following scenarios.

Example 5: Union and Intersection with List Comprehensions

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = {x for x in set1}.union([x for x in set2])
intersection_set = {x for x in set1}.intersection([x for x in set2])
print('Union:', union_set)
print('Intersection:', intersection_set)
# Output: Union: {1, 2, 3, 4, 5}
#        Intersection: {3}

This advanced example demonstrates how to use list comprehensions with union and intersection operations to allow for more dynamic set operations.

Example 6: Union and Intersection with Filtered Conditions

set1 = {x for x in range(10) if x % 2 == 0}
set2 = {x for x in range(5, 15) if x % 3 == 0}
union_set = set1.union(set2)
intersection_set = set1.intersection(set2)
print('Union:', union_set)
print('Intersection:', intersection_set)
# Output: Union: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12}
#        Intersection: {6, 12}

This example showcases how to create sets with conditional logic and then apply union and intersection operations, demonstrating both the power of set comprehension and set operations in Python.

Conclusion

Set operations such as union and intersection are powerful tools in Python’s data manipulation arsenal. Starting with basic operations and advancing through more complex examples demonstrates the versatility and utility of sets in various scenarios. Whether you are analyzing data, filtering information, or simply manipulating collections, understanding these operations can lead to more efficient and effective programming.