How to Join Sets in Python

Learn various methods to join sets in Python. Explore how to use the union() and update() methods to combine sets, the intersection() method to find common items, and the difference() method to identify unique items in the first set.



Python - Join Sets

Python - Joining Sets

In Python, you can join two or more sets in various ways.

  • Union and Update Methods: The union() and update() methods combine all items from both sets.
  • Intersection Method: The intersection() method retains ONLY the duplicate items from both sets.
  • Difference Method: The difference() method keeps the items from the first set that are not present in the other set(s).
  • Symmetric Difference Method: The symmetric_difference() method retains all items EXCEPT the duplicates.

Union

The union() method returns a new set containing all elements from both sets.

Example

      set1 = {"a", "b", "c"}
      set2 = {4, 5, 6}
      
      set3 = set1.union(set2)
      print(set3)
      
Output

      {'a', 'c', 4, 5, 6, 'b'}
      

You can also use the | operator to achieve the same result.

Example

      set1 = {"x", "y", "z"}
      set2 = {7, 8, 9}
      
      set3 = set1 | set2
      print(set3)
      
Output

      {'z', 8, 9, 'y', 7, 'x'}
      

Joining Multiple Sets

Methods and operators for joining sets can handle multiple sets.

Use union() with multiple sets by adding them as arguments:

Example

      set1 = {"m", "n", "o"}
      set2 = {10, 11}
      set3 = {"Emily", "James"}
      set4 = {"pear", "peach", "plum"}
      
      myset = set1.union(set2, set3, set4)
      print(myset)
      
Output

      {10, 11, 'peach', 'Emily', 'plum', 'James', 'm', 'o', 'pear', 'n'}
      

Using the | operator with multiple sets:

Example

      set1 = {"p", "q", "r"}
      set2 = {12, 13}
      set3 = {"Anna", "Chris"}
      set4 = {"grape", "melon", "berry"}
      
      myset = set1 | set2 | set3 | set4
      print(myset)
      
Output

      {12, 'melon', 13, 'Chris', 'grape', 'r', 'berry', 'Anna', 'p', 'q'}
      

Joining a Set and a Tuple

The union() method can join sets with other data types like tuples.

Example

      x = {"d", "e", "f"}
      y = (14, 15, 16)
      
      z = x.union(y)
      print(z)
      
Output

      {14, 'f', 15, 'e', 16, 'd'}
      

Note: The | operator only works with sets, not other data types.

Update

The update() method inserts all items from one set into another, modifying the original set.

Example

      set1 = {"g", "h", "i"}
      set2 = {17, 18, 19}
      
      set1.update(set2)
      print(set1)
      
Output

      {17, 18, 19, 'g', 'h', 'i'}
      

Intersection

The intersection() method returns a new set containing only items present in both sets.

Example

      set1 = {"apple", "banana", "cherry"}
      set2 = {"google", "microsoft", "apple"}
      
      set3 = set1.intersection(set2)
      print(set3)
      
Output

      {'apple'}
      

Using the & operator for the same result:

Example

      set1 = {"orange", "banana", "grape"}
      set2 = {"banana", "apple", "orange"}
      
      set3 = set1 & set2
      print(set3)
      
Output

      {'orange', 'banana'}
      

Note: The & operator only works with sets.

The intersection_update() method keeps only the duplicates, modifying the original set.

Example

      set1 = {"mango", "banana", "cherry"}
      set2 = {"banana", "mango", "apple"}
      
      set1.intersection_update(set2)
      print(set1)
      
Output

      {'banana', 'mango'}
      

Difference

The difference() method returns a new set with items in the first set but not in the second set.

Example

      set1 = {"grape", "banana", "cherry"}
      set2 = {"google", "microsoft", "grape"}
      
      set3 = set1.difference(set2)
      print(set3)
      
Output

      {'banana', 'cherry'}
      

Using the - operator for the same result:

Example

      set1 = {"peach", "banana", "plum"}
      set2 = {"banana", "apple", "peach"}
      
      set3 = set1 - set2
      print(set3)
      
Output

      {'plum'}
      

Note: The - operator only works with sets.

The difference_update() method keeps only the unique items in the first set, modifying the original set.

Example

      set1 = {"pear", "banana", "cherry"}
      set2 = {"banana", "apple", "pear"}
      
      set1.difference_update(set2)
      print(set1)
      
Output

      {'cherry'}
      

Symmetric Difference

The symmetric_difference() method returns a new set with items not present in both sets.

Example

      set1 = {"apple", "banana", "cherry"}
      set2 = {"google", "microsoft", "apple"}
      
      set3 = set1.symmetric_difference(set2)
      print(set3)
      
Output

      {'google', 'microsoft', 'cherry', 'banana'}
      

Using the ^ operator for the same result:

Example

      set1 = {"apple", "banana", "cherry"}
      set2 = {"google", "microsoft", "apple"}
      
      set3 = set1 ^ set2
      print(set3)
      
Output

      {'google', 'microsoft', 'cherry', 'banana'}
      

Note: The ^ operator only works with sets.

The symmetric_difference_update() method modifies the original set, keeping items not present in both sets.

Example

      set1 = {"pear", "banana", "cherry"}
      set2 = {"banana", "apple", "pear"}
      
      set1.symmetric_difference_update(set2)
      print(set1)
      
Output

      {'cherry', 'apple'}