Logo
Python Introduction
Python Installation and Project Setup
Running Python Programs
Python Syntax and Indentation Rules
Python Variables
Python Comments
Python Data Types
Python Type Conversion and Type Checking
Python Input and Output Functions
Python Operators
Python Arithmetic Operators
Python Assignment Operators
Python Logical Operators
Python Comparison Operators
Python Bitwise Operators
Python Membership Operators
Python Identity Operators
Python Walrus Operator
Python Operator Precedence
Python Conditional Statements
Python if Statement
Python if else
Python if elif else
Python match case Statement
Python Loops
Python for Loop
Python for else Loop
Python while Loop
Python break statement
Python continue statement
Python pass statement
Python Strings
Python String Slicing
Python String Concatenation
Python String Formatting
Python Escape Characters
Python Lists
Python Access List Items
Python Add List Items
Python Change List Items
Python Remove List Items
Python Sort Lists
Python Copy Lists
Python Join Lists
Python List Methods
Python Tuples
Python Access Tuple Items
Python Update Tuples
Python Unpack Tuples
Python Loop Tuples
Python Join Tuples
Python Tuple Methods
Python NamedTuple
Python Sets
Python Access Set Items
Python Add Set Items
Python Remove Set Items
Python Join Sets
Python Copy Sets
Python Dictionaries
Python Functions
Python Lambda Functions
Python Higher Order Functions
Python Classes and Objects
Python OOP Principles
Python Magic Methods
Python Context Managers
Python Error Handling and Debugging
Python File Handling
Python Modules and Packages
Python Iterators and Generators

Python Join Sets

When you work with multiple sets in Python, you often need to join sets together to create a new collection. Python join sets operations let you combine, merge, and bring together elements from two or more sets using built-in methods and operators. Whether you want every element from both sets or only the ones they share, Python gives you simple and powerful ways to join sets and get exactly the result you need.

How to Join Sets in Python Using union()

The union() method is the most common way to join sets in Python. It returns a new set that contains all the elements from both sets, with duplicates automatically removed. The original sets stay unchanged.

python
fruits = {"apple", "banana", "cherry"}
vegetables = {"carrot", "pea", "spinach"}

combined = fruits.union(vegetables)
print(combined)
Output
{'apple', 'banana', 'cherry', 'carrot', 'pea', 'spinach'}

The union method created a brand new set with every element from both the fruits and vegetables sets. Since sets are unordered, the output order may vary each time you run the code.

You can also join sets that have overlapping elements. When both sets contain the same item, the union result includes it only once because sets never allow duplicate values.

python
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}

result = set_a.union(set_b)
print(result)
Output
{1, 2, 3, 4, 5, 6}

The numbers 3 and 4 appear in both sets, but the joined result contains each number only once. This makes union perfect for merging collections where you want all unique elements.

Join Multiple Sets with union()

You can join more than two sets at once by passing multiple sets to the union method. This is useful when you need to combine several collections into one.

python
primary = {"red", "blue", "yellow"}
secondary = {"green", "orange", "purple"}
neutral = {"black", "white", "gray"}

all_colors = primary.union(secondary, neutral)
print(all_colors)
Output
{'red', 'blue', 'yellow', 'green', 'orange', 'purple', 'black', 'white', 'gray'}

All three sets were joined into a single set containing every color. The union method accepts any number of sets as arguments, making it easy to merge multiple collections in one call.

Using the Pipe Operator to Join Sets

Python provides a shorthand operator for joining sets. The pipe symbol works exactly like the union method and gives you a cleaner syntax for combining sets.

python
even = {2, 4, 6, 8}
odd = {1, 3, 5, 7}

all_numbers = even | odd
print(all_numbers)
Output
{1, 2, 3, 4, 5, 6, 7, 8}

The pipe operator joined both sets and returned a new set with all elements. You can chain multiple pipe operators to join several sets together.

python
a = {1, 2}
b = {3, 4}
c = {5, 6}

merged = a | b | c
print(merged)
Output
{1, 2, 3, 4, 5, 6}

Join Sets In-Place Using update()

The update() method joins sets by adding all elements from one set into another. Unlike union, update modifies the original set instead of creating a new one.

python
colors = {"red", "green"}
more_colors = {"blue", "yellow"}

colors.update(more_colors)
print(colors)
Output
{'red', 'green', 'blue', 'yellow'}

The colors set was modified directly to include all elements from more_colors. This is useful when you want to combine sets without creating a separate variable for the result.

The update method also accepts multiple arguments and can take any iterable, not just sets. You can join a list, tuple, or other iterable into a set using update.

python
my_set = {1, 2, 3}
my_list = [4, 5, 6]
my_tuple = (7, 8, 9)

my_set.update(my_list, my_tuple)
print(my_set)
Output
{1, 2, 3, 4, 5, 6, 7, 8, 9}

The update method merged both the list and tuple into the original set. This flexibility makes update a great choice when you need to join different types of collections into a set.

Join Sets Using intersection()

The intersection() method joins two sets by keeping only the elements that exist in both sets. This gives you a new set with just the common elements.

python
students_math = {"Alice", "Bob", "Charlie", "Diana"}
students_science = {"Bob", "Diana", "Eve", "Frank"}

both_classes = students_math.intersection(students_science)
print(both_classes)
Output
{'Bob', 'Diana'}

Only Bob and Diana appear in both sets, so the intersection result contains just those two names. The original sets remain unchanged.

You can also use the ampersand operator as a shorthand for intersection when you want to join sets and keep only shared elements.

python
set_x = {10, 20, 30, 40, 50}
set_y = {30, 40, 50, 60, 70}

common = set_x & set_y
print(common)
Output
{40, 50, 30}

Join Sets Using intersection_update()

The intersection_update() method works like intersection but modifies the original set instead of returning a new one. It keeps only elements found in all specified sets.

python
team_a = {"player1", "player2", "player3", "player4"}
team_b = {"player2", "player4", "player5", "player6"}

team_a.intersection_update(team_b)
print(team_a)
Output
{'player2', 'player4'}

The team_a set was modified to contain only the players that also exist in team_b. This is an efficient way to join sets when you only care about the overlapping elements and want to update a set in place.

Join Sets Using difference()

The difference() method returns a new set containing elements that are in the first set but not in the second. This lets you join sets in a way that filters out shared items.

python
all_employees = {"Alice", "Bob", "Charlie", "Diana", "Eve"}
remote_employees = {"Bob", "Eve"}

office_employees = all_employees.difference(remote_employees)
print(office_employees)
Output
{'Alice', 'Charlie', 'Diana'}

The result contains only employees from all_employees who are not in remote_employees. The minus operator provides the same functionality in a shorter form.

python
numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
evens = {2, 4, 6, 8, 10}

odds = numbers - evens
print(odds)
Output
{1, 3, 5, 7, 9}

Join Sets Using symmetric_difference()

The symmetric_difference() method returns a new set with elements that are in either set but not in both. Think of it as the opposite of intersection.

python
group_one = {"apple", "banana", "cherry", "date"}
group_two = {"cherry", "date", "elderberry", "fig"}

unique_items = group_one.symmetric_difference(group_two)
print(unique_items)
Output
{'apple', 'banana', 'elderberry', 'fig'}

Cherry and date appear in both sets, so they were excluded from the result. Only elements unique to one set or the other made it into the joined result. The caret operator provides the same operation.

python
left = {100, 200, 300, 400}
right = {300, 400, 500, 600}

exclusive = left ^ right
print(exclusive)
Output
{100, 200, 500, 600}

Join Sets Using symmetric_difference_update()

The symmetric_difference_update() method modifies the original set to contain only elements found in either set but not in both. It combines the sets in place without creating a new set.

python
basket_a = {"mango", "grape", "kiwi", "peach"}
basket_b = {"kiwi", "peach", "melon", "plum"}

basket_a.symmetric_difference_update(basket_b)
print(basket_a)
Output
{'mango', 'grape', 'melon', 'plum'}

The basket_a set was updated to hold only the elements that were not shared between both baskets.

Join Sets Using difference_update()

The difference_update() method removes all elements from the first set that also exist in the second set. It modifies the set in place and is the in-place version of difference.

python
inventory = {"laptop", "mouse", "keyboard", "monitor", "headset"}
sold_items = {"mouse", "headset"}

inventory.difference_update(sold_items)
print(inventory)
Output
{'laptop', 'keyboard', 'monitor'}

The sold items were removed from the inventory set directly.

Complete Working Example

This example demonstrates every way to join sets in Python, bringing together union, intersection, difference, and symmetric difference operations.

python
frontend = {"HTML", "CSS", "JavaScript", "React"}
backend = {"Python", "JavaScript", "Node", "Django"}
database = {"SQL", "MongoDB", "Redis"}

all_skills = frontend.union(backend, database)
print("Union (all skills):", all_skills)

shared_skills = frontend.intersection(backend)
print("Intersection (shared):", shared_skills)

frontend_only = frontend.difference(backend)
print("Difference (frontend only):", frontend_only)

exclusive_skills = frontend.symmetric_difference(backend)
print("Symmetric difference (exclusive):", exclusive_skills)

combined = frontend | backend | database
print("Pipe operator (all skills):", combined)

common = frontend & backend
print("Ampersand operator (shared):", common)

unique = frontend ^ backend
print("Caret operator (exclusive):", unique)

web_skills = {"HTML", "CSS"}
web_skills.update(backend)
print("Update (web_skills modified):", web_skills)

full_stack = frontend.copy()
full_stack.update(backend, database)
print("Full stack skills:", full_stack)

overlap = frontend.copy()
overlap.intersection_update(backend)
print("Intersection update:", overlap)

non_shared = frontend.copy()
non_shared.symmetric_difference_update(backend)
print("Symmetric difference update:", non_shared)

remaining = frontend.copy()
remaining.difference_update(backend)
print("Difference update:", remaining)
Output
Union (all skills): {'CSS', 'Redis', 'SQL', 'React', 'Node', 'Django', 'JavaScript', 'HTML', 'Python', 'MongoDB'}
Intersection (shared): {'JavaScript'}
Difference (frontend only): {'CSS', 'React', 'HTML'}
Symmetric difference (exclusive): {'CSS', 'React', 'Node', 'Django', 'HTML', 'Python'}
Pipe operator (all skills): {'CSS', 'Redis', 'SQL', 'React', 'Node', 'Django', 'JavaScript', 'HTML', 'Python', 'MongoDB'}
Ampersand operator (shared): {'JavaScript'}
Caret operator (exclusive): {'CSS', 'React', 'Node', 'Django', 'HTML', 'Python'}
Update (web_skills modified): {'CSS', 'Node', 'Django', 'JavaScript', 'HTML', 'Python'}
Full stack skills: {'CSS', 'Redis', 'SQL', 'React', 'Node', 'Django', 'JavaScript', 'HTML', 'Python', 'MongoDB'}
Intersection update: {'JavaScript'}
Symmetric difference update: {'CSS', 'React', 'Node', 'Django', 'HTML', 'Python'}
Difference update: {'CSS', 'React', 'HTML'}