Ways to Merge Sets in Python


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.

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.

Python Set Union

The union() method

In [1]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

all_brands = mobile_brands.union(laptop_brands)
print(all_brands)
{'Samsung', 'HTC', 'Dell', 'Blackburry', 'Apple', 'HP'}

The | Operator

In [2]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

all_brands = mobile_brands | laptop_brands
print(all_brands)
{'Samsung', 'HTC', 'Dell', 'Blackburry', 'Apple', 'HP'}

The update() method

In [3]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

mobile_brands.update(laptop_brands)
print(mobile_brands)
{'Samsung', 'HTC', 'Dell', 'Blackburry', 'Apple', 'HP'}

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.

In [1]:
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)
{'Apple', 'Motorola', 'Samsung', 'Blackburry', 'Acer', 'HP', 'HTC', 'Dell'}

Union – Way 2.

In [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)
{'Apple', 'Motorola', 'Samsung', 'Blackburry', 'Acer', 'HP', 'HTC', 'Dell'}

Update with | operator.

In [3]:
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)
{'Apple', 'Motorola', 'Samsung', 'Blackburry', 'Acer', 'HP', 'HTC', 'Dell'}

Update with multiple arguments.

In [4]:
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)
{'Apple', 'Motorola', 'Samsung', 'Blackburry', 'Acer', 'HP', 'HTC', 'Dell'}

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

In [1]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

common_brands = mobile_brands.intersection(laptop_brands)
print(common_brands)
{'Samsung', 'Apple'}

The & operator

In [2]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

common_brands = mobile_brands & laptop_brands
print(common_brands)
{'Samsung', 'Apple'}

The intersection_update() method

In [3]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

mobile_brands.intersection_update(laptop_brands)
print(mobile_brands)
{'Samsung', 'Apple'}

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

In [1]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

only_mobile_brands = mobile_brands.difference(laptop_brands)
print(only_mobile_brands)
{'Blackburry', 'HTC'}

The – operator

In [2]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

only_mobile_brands = mobile_brands - laptop_brands
print(only_mobile_brands)
{'Blackburry', 'HTC'}

The difference_update() method

In [3]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

mobile_brands.difference_update(laptop_brands)
print(mobile_brands)
{'Blackburry', 'HTC'}

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

In [1]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

uncommon_brands = mobile_brands.symmetric_difference(laptop_brands)
print(uncommon_brands)
{'Dell', 'HTC', 'HP', 'Blackburry'}

The ^ operator

In [2]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

uncommon_brands = mobile_brands ^ laptop_brands
print(uncommon_brands)
{'Dell', 'HTC', 'HP', 'Blackburry'}

The symmetric_difference_update() method

In [3]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

mobile_brands.symmetric_difference_update(laptop_brands)
print(mobile_brands)
{'Dell', 'HTC', 'HP', 'Blackburry'}

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.

In [1]:
mobile_brands = {"Samsung", "Apple", "HTC", "Blackburry"}
laptop_brands = {"Samsung", "Apple", "HP", "Dell"}

all_brands = (*mobile_brands, *laptop_brands)
print(all_brands)
('HTC', 'Apple', 'Blackburry', 'Samsung', 'HP', 'Apple', 'Samsung', 'Dell')

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.

In [1]:
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)
{'Dell', 'HTC', 'Blackburry', 'Apple', 'HP', 'Samsung'}

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.

In [1]:
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)
{'Acer', 'Dell', 'Motorola', 'HP', 'Apple', 'Samsung', 'HTC', 'Blackburry'}

References:

  1. Python Join Two Sets
  2. Join two Sets in Python