Python Tutorial

Joining Two Sets in Python With Examples

How to Join Two Sets in Python?

In Python, there are numerous techniques to merge two or more sets.

You can use the union() function to create a new set that has all of the items from both sets, or you can use the update() method to insert all of the things from one set into another.

Code:

x = {"a", "b" , "c"}

y = {1, 2, 3}

z = x.union(y)

print(z)

Output:

{2, 'b', 1, 'c', 'a', 3}

Keep Only the Duplicates

The elements present in both sets will be kept by the intersection update() function.

Code:

x = {1,2,3}

y = {3,4,5}

x.intersection_update(y)

print(x)

Output:

3
Did you find this article helpful?