Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Join Sets

Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Join Sets

Joining Sets in Python

Python sets behave a lot like mathematical sets. You can combine, intersect, and compare them using different methods. Let’s walk through how you can join sets effectively.

Union: union() vs update()

Both methods are used to combine two sets. But here’s the key difference:

  • union() creates a new set with all unique items from both.

  • update() adds items from the second set into the first on

Example 1: Using union()
				
					countries_a = {"Pakistan", "India", "Nepal"}
countries_b = {"India", "Bhutan", "Sri Lanka"}

combined = countries_a.union(countries_b)
print(combined)

				
			

Output:

				
					{'Nepal', 'Pakistan', 'Bhutan', 'India', 'Sri Lanka'}

				
			
Example 2: Using update()
				
					countries_a = {"Pakistan", "India", "Nepal"}
countries_b = {"India", "Bhutan", "Sri Lanka"}

countries_a.update(countries_b)
print(countries_a)

				
			

Output:

				
					{'Nepal', 'Bhutan', 'Pakistan', 'India', 'Sri Lanka'}

				
			

Notice how update() modifies the original set directly.

Intersection: intersection() vs intersection_update()

These methods help find common items in both sets.

  • intersection() returns a new set of common elements.

  • intersection_update() updates the first set with only common items.

Example 1: Using intersection()
				
					group1 = {"Ali", "Sara", "Zain", "Hina"}
group2 = {"Zain", "Hina", "Tariq"}

common = group1.intersection(group2)
print(common)

				
			

Output:

				
					{'Zain', 'Hina'}

				
			
Example 2: Using intersection_update()
				
					group1 = {"Ali", "Sara", "Zain", "Hina"}
group2 = {"Zain", "Hina", "Tariq"}

group1.intersection_update(group2)
print(group1)

				
			

Output:

				
					{'Zain', 'Hina'}


				
			

Symmetric Difference: symmetric_difference() vs symmetric_difference_update()

These methods return all uncommon items — present in either of the sets but not both.

  • symmetric_difference() gives you a new set.

  • symmetric_difference_update() modifies the original.

Example 1: Using symmetric_difference()
				
					batch1 = {"Red", "Blue", "Green"}
batch2 = {"Blue", "Yellow", "Orange"}

unique_colors = batch1.symmetric_difference(batch2)
print(unique_colors)

				
			

Output:

				
					{'Green', 'Orange', 'Red', 'Yellow'}

				
			
Example 2: Using symmetric_difference_update()
				
					batch1 = {"Red", "Blue", "Green"}
batch2 = {"Blue", "Yellow", "Orange"}

batch1.symmetric_difference_update(batch2)
print(batch1)

				
			

Output:

				
					{'Green', 'Yellow', 'Red', 'Orange'}

				
			

Difference: difference() vs difference_update()

These help find items that are only in the first set and not in the second one.

  • difference() returns a new set.

  • difference_update() removes common items from the original set.

Example 1: Using difference()
				
					team_a = {"Ahmed", "Bilal", "Usman", "Raza"}
team_b = {"Usman", "Raza"}

only_in_a = team_a.difference(team_b)
print(only_in_a)

				
			

Output:

				
					{'Bilal', 'Ahmed'}

				
			
Example 2: Using difference_update()
				
					team_a = {"Ahmed", "Bilal", "Usman", "Raza"}
team_b = {"Usman", "Raza"}

team_a.difference_update(team_b)
print(team_a)

				
			

Output:

				
					{'Ahmed', 'Bilal'}


				
			

Summary

  • union() and update() add all unique items from two sets.

  • intersection() and intersection_update() keep only items common in both sets.

  • symmetric_difference() and symmetric_difference_update() keep only items not common in both.

  • difference() and difference_update() keep only items unique to the first set

Use whichever fits your need — whether to return a new set or update an existing o

Keep practicing — only at Learn With Arshyan.

Scroll to Top