Merge Sets / Join Sets
Python provides us with various built-in methods to merge sets. Sets are very much useful when we have to perform mathematical operations like union, intersection, difference, symmetric difference, etc. In this tutorial, we will learn about the different approaches to merge sets.
Union
The union operation is used to return all the elements of both the sets. For union, we can either use the union() method or the | operator. These will return the new set.
We can also use the update() method to update one set with items of the second set.
The union() method
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
all_brands = mobile_brands.union(laptop_brands)
print(all_brands)
The | Operator
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
all_brands = mobile_brands | laptop_brands
print(all_brands)
The update() method
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
mobile_brands.update(laptop_brands)
print(mobile_brands)
Multiple Sets
These operations can also be performed with multiple sets. Here are a few examples when we have three sets:
Union with multiple arguments.
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
tablet_brands = {"Samsung", "Acer", "Motorola"}
all_brands = mobile_brands.union(laptop_brands, tablet_brands)
print(all_brands)
Union – Way 2.
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
tablet_brands = {"Samsung", "Acer", "Motorola"}
all_brands = set().union(mobile_brands).union(laptop_brands).union(tablet_brands)
print(all_brands)
Update with | operator.
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
tablet_brands = {"Samsung", "Acer", "Motorola"}
all_brands = mobile_brands | laptop_brands | tablet_brands
print(all_brands)
Update with multiple arguments.
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
tablet_brands = {"Samsung", "Acer", "Motorola"}
mobile_brands.update(laptop_brands, tablet_brands)
print(mobile_brands)
Intersection
The intersection operation is used to find the common items between both the sets. For this, we can either use the intersection() method or the & operator. These will return the new set.
We can use intersection_update to update one set with common items of both sets.
The intersection() method
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
common_brands = mobile_brands.intersection(laptop_brands)
print(common_brands)
The & operator
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
common_brands = mobile_brands & laptop_brands
print(common_brands)
The intersection_update() method
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
mobile_brands.intersection_update(laptop_brands)
print(mobile_brands)
Difference
The difference operation is used to find items of set A that are not present in set B, means unique items of set A. For this, we can either use the difference() method or the – operator.
We can use the difference_update() method to update one set with a difference.
The difference() method
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
only_mobile_brands = mobile_brands.difference(laptop_brands)
print(only_mobile_brands)
The – operator
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
only_mobile_brands = mobile_brands - laptop_brands
print(only_mobile_brands)
The difference_update() method
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
mobile_brands.difference_update(laptop_brands)
print(mobile_brands)
Symmetric Difference
The symmetric difference operation is used to find items that are not common in both the sets. For this, we either use the symmetric_difference() method or the ^ operator.
The symmetric_difference_update() method can be used to update one set with symmetric difference.
The symmetric_difference() method
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
uncommon_brands = mobile_brands.symmetric_difference(laptop_brands)
print(uncommon_brands)
The ^ operator
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
uncommon_brands = mobile_brands ^ laptop_brands
print(uncommon_brands)
The symmetric_difference_update() method
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
mobile_brands.symmetric_difference_update(laptop_brands)
print(mobile_brands)
Using the unpacking * operator
The unpack operator, i.e. (*) asterisk can be used to join two sets. This is simple and easy approach works in the same way as we unpack tuples.
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
all_brands = (*mobile_brands, *laptop_brands)
print(all_brands)
Using the Itertools.chain() method
The itertools.chain() is a function in the itertools module of Python that is used to combine multiple iterables into a single iterable. It takes any number of iterable arguments. The itertools.chain() returns an iterator, so if we want to get a set or other data structure, we have to convert it.
import itertools
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}
all_brands = set(itertools.chain(mobile_brands, laptop_brands))
print(all_brands)
The itertools.chain() is useful when we have to join multiple types of iterators. For example, a list, a set and a tuple are joined in the below example.
import itertools
brands_list = ["Samsung", "Acer", "Motorola"]
brands_tuple = ("Samsung", "Apple", "HP", "Dell")
brands_set = {"Samsung", "Apple", "HTC", "Blackburry"}
all_brands = set(itertools.chain(brands_list, brands_tuple, brands_set))
print(all_brands)