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

Shahnawaz KhanShahnawaz Khan
2 min read

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 the other set. The original set remains unchanged.

  • difference_update(other)
    Updates the original set in-place, removing all elements found in the other 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() and intersection() return new sets, leaving the original unchanged.

  • Their counterparts difference_update() and intersection_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 like collections.OrderedDict can help.

By understanding these differences, you can write cleaner, more predictable Python code for set operations and tuple manipulations.

0
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.