Learn How to Combine Sets in Python

In python, a set is an unordered collection of items. The items in a set are unordered and thus cannot be accessed with indexes. Sets come in handy while performing mathematical operations in python. The operations are union, intersection, complement difference, etc. In this article, we shall learn about sets and how we can combine two sets in python.

Characteristics of a set

  • All the items in a set are unique. Sets do not store duplicate elements.
  • The elements in a set are unordered. Therefore, the items cannot be accessed with indexes, unlike tuples and lists—every time the elements are displayed in random order.
  • The set itself is mutable, but the elements stored in the set are immutable.

How to create a set?

A set in python is initialized by using curly braces({ }). Thus, we can store different elements in a python set except for mutable data types such as lists and dictionaries.

my_set = { 1, 'ab', 100}

To create an empty set, we use the function set() without giving it any arguments. By default, if you use curly braces to create an empty set, python would consider it a dictionary.

my_set = set()

How to Combine Sets in Python?

Sets can be combined in python using different functions. There are functions such as union(), update() and reduce() for joining two sets. We can also combine two sets using bitwise operators such as the union operator (|) and the unpacking operator (*). Let us look at all the methods separately.

Using union() method to combine sets in python

Union() method is used to combine two sets in python and returns a combined set. We can perform a union of as many sets as we want. If two sets contain common items, then their union will only have one copy of that item because all elements in a set are unique.

The syntax of union() method is:

set1.union(set2, set3, set4......)

Lets us consider an example. Here, we have taken three sets: set1, set2, and set3. ‘set1’ has three strings – harry, hogwarts and hedwig. ‘set2’ has one float value – 9.75 and three strings – wand, hogwarts and london. ‘set3’ consists of one character value – h and one integer value – 100.

Here, only set1 and set2 have a common item – ‘hogwarts’ and rest other items are unique.

To perform union of set1 and set2, we run the below code.

set1.union(set2)

The output of the union between set1 and set2 is:

{9.75, 'harry', 'hedwig', 'hogwarts', 'london', 'wand'}

As you can see, here the common element ‘hogwarts’ only occurred once in the union set.

To perform union between set1, set2 and set3, we use the code given below.

set1.union(set2,set3)

The combination of the three sets is:

{100, 9.75, 'h', 'harry', 'hedwig', 'hogwarts', 'london', 'wand'}

Here, changing the order of sets while using the union() method does not change the output.

set3.union(set2,set1)

The above line of code also gives us the same result.

{100, 9.75, 'h', 'harry', 'hedwig', 'hogwarts', 'london', 'wand'}

If we do not pass any argument to the union method, it shall return a shallow copy of the given set.

Using update() Method to Combine Sets in Python

Another method that can be used to join two or more sets in python is the update() method. Finally, the union() method will store all the values uniquely from two or more given sets.

The syntax of update() method is:

set1.update(set2,set3,set4....) 

The difference between union() and update() method is that union() returns the combined set but update() will not return any value. Instead, it will only update the value of the set calling the update() method (i.e., set1 from the above syntax) with the value of the set or sets given in the argument (i.e., set2, set3, set4…).

Let us take the same example of the above sets set1, set2 and set3.

set1 = {'hogwarts', 'harry', 'hedwig'}
set2 = {'wand', 9.75, 'hogwarts', 'london'}
set3 = {100, 'h'}

First we shall combine two sets – set1 and set2.

set1.update(set2)

The above code will not return any value. To view the update, we shall print set1.

print(set1)

The output is:

{'wand', 'hedwig', 9.75, 'london', 'harry', 'hogwarts'}

set2 remains unchanged whereas set1 gets updated with the combined values.

To combine all the three sets, we shall run the below code.

set1.update(set2,set3)

Now, we shall print the updated set1.

print(set1)

The output is:

{'wand', 'hedwig', 100, 9.75, 'london', 'harry', 'hogwarts', 'h'}

As we can see, all the set values have been combined. Therefore, we can also pass iterables as an argument to the update() function.

Using reduce() Method to Combine Sets in Python

Reduce() is used to apply a function to iterables to reduce it into a single value. We can also use reduce() to combine two given sets.

For that, we need to import an operator. An operator is a library in python which imports functions for mathematical, logical, and bitwise operations. For combining sets, we import operators for performing bitwise or operation.

The syntax of reduce() function is:

reduce(function, iterable)

First, we shall import functools and operator.

from functools import reduce
import operator
set1 = {'hogwarts', 'harry', 'hedwig'}
set2 = {'wand', 9.75, 'hogwarts', 'london'}
set3 = {100, 'h'}

We shall use the operator.or_ as the function, and we shall pass a list of set1 and set2 as the iterable sequence.

reduce(operator.or_, [set1, set2])

The function reduce() shall return the bitwise or operation of set1 and set2. Operator.or_ shall work similarly to the union operation. The output will be:

{9.75, 'harry', 'hedwig', 'hogwarts', 'london', 'wand'}

We can also use reduce() for combining more than two sets. We can pass the more than two sets by including them into the iterable list.

reduce(operator.or_, [set1, set2, set3])

The output will be a combined set.

{100, 9.75, 'h', 'harry', 'hedwig', 'hogwarts', 'london', 'wand'}

Using itertools.chain() Method to Combine Sets in Python

Itertools in python is a module that has many functions used for working with iterators. chain() is one such function available in the itertools module. Itertools.chain() is used to chain multiple iterables and returns a single sequence.

The syntax for itertools.chain() is:

itertools.chain(set1,set2,set3....)

Here, we use itertools.chain() to combine two or more sets together.

Taking set1, set2 an set3 as above.

set1 = {'hogwarts', 'harry', 'hedwig'}
set2 = {'wand', 9.75, 'hogwarts', 'london'}
set3 = {100, 'h'}

First, we shall import itertools.

import itertools

Now, we shall pass set1, set2, and set3 as arguments to itertools.chain(). The function returns an object which we explicitly convert into a type set. Then, we shall print that set.

print(set(itertools.chain(set1,set2,set3)))

The combined set is:

{'wand', 'hedwig', 100, 9.75, 'harry', 'london', 'hogwarts', 'h'}

Using ‘|’ operator

We can also combine two or more sets in python using bitwise or operator (|). It is similar to finding the union of sets.

Taking three sets set1, set2 and set3.

set1 = {'hogwarts', 'harry', 'hedwig'}
set2 = {'wand', 9.75, 'hogwarts', 'london'}
set3 = {100, 'h'}

Performing bitwise or operation:

print(set1 | set2 | set3)

The output is:

{'wand', 'hedwig', 100, 'hogwarts', 9.75, 'london', 'h', 'harry'}

The above operation is similar to the mathematical union operation.

'set1 | set2 | set3' is similar to 'set1 U set2 U set3'

Using unpacking operator (*)

In python, the asterisk (*) is the unpacking operator. It unpacks values from iterable objects. We can use the unpacking operator for joining multiple sets.

Taking set1, set2 and set3.

set1 = {'hogwarts', 'harry', 'hedwig'}
set2 = {'wand', 9.75, 'hogwarts', 'london'}
set3 = {100, 'h'}

Now we shall unpack the three sets and assign the combined value to a new set ‘my_set’.

my_set = {*set1, *set2, *set3}

And now, we shall print the final set ‘my_set’.

print(my_set)

The combined set is:

{'wand', 'hedwig', 100, 'hogwarts', 9.75, 'london', 'h', 'harry'}

Do you have any doubts or any ideas to share? Let us know below in the comments.

Learning never exhausts the mind. Happy Learning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments