Post

Python Set update() Method

In this tutorial, we will understand about the python set update() method and its uses.

Python Set update() Method

The update() method adds elements from another set (or any iterable) to the original set in place. Duplicate elements are automatically ignored, since sets only store unique values. It can also be written using the |= operator.

Syntax

1
2
3
set.update(iterable)
# or
set1 |= set2

Parameters

ParameterDescription
iterableOne or more sets or iterables whose elements are added to the set.

Return Value

None — the original set is modified in place.


Example 1: Basic Update

1
2
3
4
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2)
print(set1)  # {1, 2, 3, 4, 5}

The shared element 3 is not duplicated.


Example 2: Using the |= Operator

1
2
3
numbers = {1, 2, 3}
numbers |= {4, 5}
print(numbers)  # {1, 2, 3, 4, 5}

Example 3: Update with Multiple Iterables

1
2
3
4
5
my_set = {1, 2, 3}
my_list = [3, 4]
my_tuple = (5, 6)
my_set.update(my_list, my_tuple)
print(my_set)  # {1, 2, 3, 4, 5, 6}

update() accepts several iterables at once.


Example 4: Update with a String

A string is iterable, so each character is added:

1
2
3
letters = {"a", "b"}
letters.update("cd")
print(letters)  # {'a', 'b', 'c', 'd'}

Example 5: Update with an Empty Iterable

1
2
3
s = {1, 2, 3}
s.update([])
print(s)  # {1, 2, 3} — unchanged

Example 6: Adding from a Range

1
2
3
nums = {0}
nums.update(range(1, 5))
print(nums)  # {0, 1, 2, 3, 4}

update() vs add()

MethodAddsUse when
add(element)A single elementAdding one item
update(iterable)Many elementsMerging an iterable
1
2
3
4
s = {1, 2}
s.add(3)            # add one
s.update([4, 5])    # add many
print(s)            # {1, 2, 3, 4, 5}

update() vs union()

MethodModifies original?Returns
update(other)YesNone
union(other)NoNew set

Use update() for in-place merging, union() when you need a new set.


Real-World Use Cases

1. Collecting unique visitors:

1
2
3
4
unique_visitors = set()
unique_visitors.update(["user1", "user2", "user1"])
unique_visitors.update(["user3"])
print(unique_visitors)  # {'user1', 'user2', 'user3'}

2. Merging tags from multiple sources:

1
2
3
tags = {"python"}
tags.update(["coding", "tutorial"], ("beginner",))
print(tags)

3. Building a vocabulary from multiple documents:

1
2
3
4
vocabulary = set()
for document in ["the cat sat", "the dog ran"]:
    vocabulary.update(document.split())
print(vocabulary)  # {'the', 'cat', 'sat', 'dog', 'ran'}

Common Mistakes

  • Expecting a return valueupdate() returns None.
  • Confusing with add()add() adds the whole argument as one element; update() iterates and adds each element.
  • Passing a single non-iterables.update(5) raises TypeError; use s.add(5) instead.

FAQ

Q: What is the difference between update() and add()? add() inserts one element; update() inserts all elements from an iterable.

Q: Can update() take more than one argument? Yes — s.update(a, b, c) merges all of them.

Q: Does update() preserve insertion order? No — sets are unordered; the printed order is not guaranteed.


Performance and Time Complexity

update() adds elements in place with time proportional to the total number of incoming elements, each inserted in average O(1). Because it mutates the existing set rather than allocating a new one, it is the most efficient way to accumulate unique values from many sources.

  • add(x) — inserts a single element.
  • union() — returns a new combined set instead of mutating.
  • intersection_update() — the filtering counterpart.

Best Practices

  1. Use |= for concise in-place merging of two sets.
  2. Pass multiple iterables at once: s.update(a, b, c).
  3. Use add() (not update()) when inserting a single non-iterable value.

Key Takeaways

update() merges all elements from one or more iterables into a set in place, ignores duplicates automatically, returns None, and is the standard way to grow a set of unique values incrementally.

A Mental Model for update()

Think of update() as topping up a jar from several other containers at once, with duplicates automatically discarded. It is the accumulation workhorse of set programming — feed it lists, tuples, generators, or other sets and it folds all of their unique values into your existing collection. This makes it ideal for building up a set of distinct items as you stream through pages of data, log lines, or database rows.

Conclusion

The update() method is a small but powerful part of Python’s set toolkit. In short, it folds the unique values of many iterables into an existing set. Sets are one of Python’s most underrated data structures: they offer average constant-time membership tests, automatic de-duplication, and a rich family of mathematical operations that map directly onto everyday programming problems such as filtering, matching, grouping, and change detection. Mastering methods like update() lets you replace verbose loops and manual bookkeeping with a single, expressive call that communicates intent clearly to anyone reading your code. Whenever you are juggling collections of unique items and find yourself writing nested conditionals to compare them, pause and ask whether a set operation would express the same logic more concisely. More often than not, the answer is yes — and update() may be exactly the tool you need.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.