Python - Join Sets



In Python, a set is an ordered collection of items. The items may be of different types. However, an item in the set must be an immutable object. It means, we can only include numbers, string and tuples in a set and not lists. Python's set class has different provisions to join set objects.

Join Sets in Python

Joining sets in Python refers to merging two or more sets as a single set. When you join sets, you merge the elements of multiple sets while ensuring that duplicate elements are removed, as sets do not allow duplicate elements.

This can be achieved using various methods, such as union, update, set comprehension, set concatenation, copying, and iterative addition.

Join Python Sets Using "|" Operator

The "|" symbol (pipe) is defined as the union operator. It performs the A∪B operation and returns a set of items in A, B or both. Set doesn't allow duplicate items.

Example

In the following example, we are performing a union operation on sets "s1" and "s2" using the "|" operator, creating a new set "s3" containing elements from both sets without duplicates −

s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = s1|s2
print (s3)

It will produce the following output −

{1, 2, 3, 4, 5, 6, 7, 8}

Join Python Sets Using union() Method

The set class has union() method that performs the same operation as | operator. It returns a set object that holds all items in both sets, discarding duplicates.

Example

In this example, we are invoking the union() method on set "s1", passing set "s2" as an argument, which returns a new set "s3" containing elements from both sets without duplicates −

s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = s1.union(s2)
print (s3)

Following is the output obtained −

{1, 2, 3, 4, 5, 6, 7, 8}

Join Python Sets Using update() Method

The update() method also joins the two sets, as the union() method. However it doen't return a new set object. Instead, the elements of second set are added in first, duplicates not allowed.

Example

In the example below, we are updating set "s1" with the elements of set "s2" using the update() method, modifying "s1" to contain elements from both sets without duplicates −

s1={1,2,3,4,5}
s2={4,5,6,7,8}
s1.update(s2)
print (s1)

The result obtained is as shown below −

{1, 2, 3, 4, 5, 6, 7, 8}

Join Python Sets Using Unpacking Operator

In Python, the "*" symbol is used as unpacking operator. The unpacking operator internally assign each element in a collection to a separate variable.

We can join Python sets using the unpacking operator (*) by unpacking the elements of multiple sets into a new set.

Example

In the following example, we are creating a new set "s3" by unpacking the elements of sets "s1" and "s2" using the * operator within a set literal −

s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = {*s1, *s2}
print (s3)

The output produced is as follows −

{1, 2, 3, 4, 5, 6, 7, 8}

Join Python Sets Using Set Comprehension

Set comprehension in Python is a concise way to create sets using an iterable, similar to list comprehension but resulting in a set instead of a list. It allows you to generate sets by applying an expression to each item in an iterable while optionally filtering the items based on a condition.

We can join python sets using set comprehension by iterating over multiple sets and adding their elements to a new set.

Example

In this example, we are creating a new set "joined_set" using a set comprehension. By iterating over a list containing "set1" and "set2", and then iterating over each element "x" within each set "s", we merge all elements from both sets into "joined_set" −

set1 = {1, 2, 3}
set2 = {3, 4, 5}

joined_set = {x for s in [set1, set2] for x in s}
print(joined_set) 

Output of the above code is as shown below −

{1, 2, 3, 4, 5}

Join Python Sets Using Iterative Addition

Iterative addition in the context of sets refers to iteratively adding elements from one set to another set using a loop or iteration construct. This allows you to merge the elements of multiple sets into a single set, ensuring that duplicate elements are not included.

We can join python sets using iterative addition by iterating over the elements of each set and adding them to a new set.

Example

In the example below, we first initialize an empty set. Then, we iterate over each element in "set1" and "set2" separately, adding each element into a new set named "joined_set" using the add() method −

set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Initializing an empty set to hold the merged elements
joined_set = set()

# Iterating over set1 and adding its elements to the joined set
for element in set1:
   joined_set.add(element)

# Iterating over set2 and adding its elements to the joined set
for element in set2:
   joined_set.add(element)

print(joined_set)  

After executing the above code, we get the following output −

{1, 2, 3, 4, 5}
Advertisements