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 β
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}