Join Sets
Sets in Python more or less work in the same way as sets in mathematics. We can perform operations like union and intersection on the sets just like in mathematics.
I. union()
and update()
:
The union()
and update()
methods print all items that are present in the two sets. The union()
method returns a new set whereas the update()
method adds items into the existing set from another set.
Example 1:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.union(cities2)
print(cities3)
Output:
{'Tokyo', 'Madrid', 'Kabul', 'Seoul', 'Berlin', 'Delhi'}
Example 2:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.update(cities2)
print(cities)
Output:
{'Berlin', 'Madrid', 'Tokyo', 'Delhi', 'Kabul', 'Seoul'}
II. intersection()
and intersection_update()
:
The intersection()
and intersection_update()
methods print only items that are similar to both sets. The intersection()
method returns a new set whereas the intersection_update()
method updates the existing set from another set.
Example 1:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.intersection(cities2)
print(cities3)
Output:
{'Madrid', 'Tokyo'}
Example 2:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.intersection_update(cities2)
print(cities)
Output:
{'Tokyo', 'Madrid'}
III. symmetric_difference()
and symmetric_difference_update()
:
The symmetric_difference()
and symmetric_difference_update()
methods print only items that are not similar to both sets. The symmetric_difference()
method returns a new set whereas the symmetric_difference_update()
method updates the existing set from another set.
Example 1:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.symmetric_difference(cities2)
print(cities3)
Output:
{'Seoul', 'Kabul', 'Berlin', 'Delhi'}
Example 2:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.symmetric_difference_update(cities2)
print(cities)
Output:
{'Kabul', 'Delhi', 'Berlin', 'Seoul'}
IV. difference()
and difference_update()
:
The difference()
and difference_update()
methods print only items that are only present in the original set and not in both sets. The difference()
method returns a new set whereas the difference_update()
method updates the existing set from another set.
Example 1:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Seoul", "Kabul", "Delhi"}
cities3 = cities.difference(cities2)
print(cities3)
Output:
{'Tokyo', 'Madrid', 'Berlin'}
Example 2:
cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Seoul", "Kabul", "Delhi"}
print(cities.difference(cities2))
Output:
{'Tokyo', 'Berlin', 'Madrid'}