CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

PYTHON - Join Sets

Python - Join Sets

In Python, asetis 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 notlists. Python’s set class has different provisions to join set objects.

Join Sets in Python

Joining sets inPythonrefers 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 AB 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 βˆ’

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

It will produce the following output βˆ’

PYTHON CODE
{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 βˆ’

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

Following is the output obtained βˆ’

PYTHON CODE
{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 βˆ’

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

The result obtained is as shown below βˆ’

PYTHON CODE
{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 βˆ’

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

The output produced is as follows βˆ’

PYTHON CODE
{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” βˆ’

PYTHON CODE
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 βˆ’

PYTHON CODE
{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 βˆ’

PYTHON CODE
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 βˆ’

PYTHON CODE
{1, 2, 3, 4, 5}