Understanding set Operations in Python: difference() vs difference_update(), intersection() vs intersection_update()

Working with sets in Python is common when handling unique collections of data. However, many learners get confused between the regular set operations (like difference()
or intersection()
) and their in-place update counterparts (like difference_update()
or intersection_update()
). In this blog, weโll clarify the differences with examples, and then walk through a practical task of merging tuples and removing duplicates.
๐น difference()
vs difference_update()
difference(other)
Returns a new set containing the elements of the current set that are not in theother
set. The original set remains unchanged.difference_update(other)
Updates the original set in-place, removing all elements found in theother
set.
Example:
a = {1, 2, 3}
b = {3, 4}
print(a.difference(b)) # {1, 2}
print(a)
# {1, 2, 3} (unchanged)
a.difference_update(b)
print(a) # {1, 2} (modified)
๐ Python docsโโโdifference
๐ Python docs โ difference_update
๐น intersection()
vs intersection_update()
intersection(other)
Returns a new set with elements common to both sets.intersection_update(other)
Updates the original set in-place with only elements that are common to both sets.
Example:
x = {1, 2, 3}
y = {2, 3, 4}
print(x.intersection(y)) # {2, 3}
print(x) # {1, 2, 3} (unchanged)
x.intersection_update(y)
print(x) # {2, 3} (modified)
๐ Python docsโโโintersection
๐ Python docs โ intersection_update
๐น Practical Task: Merging Tuples and Removing Duplicates
Suppose you are given two tuples:
tup1 = (1, 2, 3)
tup2 = (3, 4, 5)
โ Solution:
# Step 1: Merge tuples
merged = tup1 + tup2 # (1, 2, 3, 3, 4, 5)
# Step 2: Convert to set to remove duplicates
unique = set(merged) # {1, 2, 3, 4, 5}
# Step 3: Convert back to tuple
final_tuple = tuple(unique)
print(final_tuple) # (1, 2, 3, 4, 5)
โ ๏ธ Note: Since sets in Python are unordered, the order of elements in the final tuple may vary.
If order matters, we can preserve it like this:
merged = tup1 + tup2
final_tuple = tuple(dict.fromkeys(merged))
print(final_tuple) # (1, 2, 3, 4, 5)
๐น Conclusion
Methods like
difference()
andintersection()
return new sets, leaving the original unchanged.Their counterparts
difference_update()
andintersection_update()
modify the set in-place.For tuple operations, using
set()
is a quick way to remove duplicates, but if order matters,dict.fromkeys()
or libraries likecollections.OrderedDict
can help.
By understanding these differences, you can write cleaner, more predictable Python code for set operations and tuple manipulations.
Subscribe to my newsletter
Read articles from Shahnawaz Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shahnawaz Khan
Shahnawaz Khan
Passionate about Software Testing, Designing Enterprise Level Software Automation Scripts, Data Science, Machine Learning, and Research are my passions. This channel is an extension of my passion for sharing my insights about the technology related to Artificial Intelligence and Computer Science. Shahnawaz Khan completed his graduation from COMSATS University, Pakistan. His master's is in Computer Science at University of Central Punjab, Pakistan. Shahnawaz has 10 years of extensive market experience and to learn more. He has shifted his career from Quality Assurance to Software Development. He is doing research focused on AI in Healthcare using ML and AI techniques. If you are passionate and eager to learn more about the technologies, it would mean the world to me if you would consider subscribing to this channel.