Python set.update() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
set method

Definition

update(*others) adds every item of each iterable you pass to the set. It changes the set in place, skips values that are already present, and returns None.

The arguments can be any iterable: a list, a tuple, another set, a range, a dict (its keys), or a string (its single characters). You may pass several at once, or none, in which case nothing happens.

set.update() is an instance method of the built-in set type: call it on a set with dot notation, such as seen.update(...). No import is needed.

Syntax

Pass zero or more iterables, separated by commas; the operator form needs a set on the right.

python
s.update(iterable)
s.update(iterable1, iterable2, iterable3)
s |= other_set
CallWhat it does
{1, 2}.update([2, 3, 4])Returns None; the set becomes {1, 2, 3, 4}. The duplicate 2 is ignored.
{1, 2}.update(range(3, 5), (6,))Returns None; the set becomes {1, 2, 3, 4, 6}. Several iterables can be passed at once.
{1, 2}.update(5)Raises TypeError: an int is not iterable.

s |= other works only when other is a set, while update() takes any iterable. To add exactly one item use add(); to get a new set instead of changing this one use union().

Method signatures
python
set.update(*others)
# returns: None; every item of every iterable is now in the set
# raises: TypeError if an argument is not iterable or an item is unhashable

Parameters

ParameterDescription
*othersZero or more iterables: list, tuple, set, string, range, dict, and so on. Every item must be hashable. A dict contributes its keys, and a string contributes its characters.

Return Value

Return ValueDescription
NoneAlways None. The set itself now holds its old items plus every item from the arguments.

Python Examples

Example 1 — Merge Email Lists Without Duplicates

Feed a list and a tuple into one set with a single call.

python
newsletter = {"[email protected]", "[email protected]"}
promo = ["[email protected]", "[email protected]", "[email protected]"]
partners = ("[email protected]", "[email protected]")

print(newsletter.update(promo, partners))
print(len(newsletter))
print(sorted(newsletter))

How It Works

  1. Two sources, one call. The list and the tuple are both passed to update(), and every address from each is added.
  2. Duplicates collapse. Ana, Raj, and Lee each appear in more than one place but are stored once, so four addresses remain.
  3. None as always. update() changes the set and returns None, which is what the first line prints.
  4. Alphabetical output. sorted() lists the addresses in a fixed order so the result is the same on every run.

Example 2 — Collect Letters, Not Words

See what a string argument does, how to add a whole word, and how range and sets can be merged.

python
letters = set()
letters.update("banana")
print(sorted(letters))

words = set()
words.update(["banana"])
words.add("cherry")
print(sorted(words))

numbers = {1, 2}
numbers.update(range(3, 6), {5, 6})
print(sorted(numbers))

How It Works

  1. A string is a row of characters. "banana" contributes b, a, and n; the repeated letters are stored once.
  2. Wrap the word in a list. update(["banana"]) iterates over a list with one item, so the whole word is added.
  3. Whole word with add(). add("cherry") adds one value without iterating over it.
  4. Mixing iterables. range(3, 6) gives 3, 4, 5 and the set gives 5, 6; the shared 5 is stored once, so the result holds 1 through 6.

Example 3 — Collect Every Skill Used in a Team

Pass two dicts to update() to gather their keys, and see what happens with a number.

python
skills = {"python"}
alice = {"sql": 3, "python": 5}
bob = {"git": 4, "sql": 2}

skills.update(alice, bob)
print(sorted(skills))

try:
    skills.update(42)
except TypeError:
    print("an int is not iterable")

print(len(skills))

How It Works

  1. Dicts give their keys. Iterating over a dict yields its keys, so only the skill names are added and the ratings are ignored.
  2. Shared keys count once. "python" and "sql" appear in more than one place, so the set ends with three skills.
  3. A number is not iterable. update(42) has nothing to loop over and raises TypeError.
  4. The set is unchanged by the error. The failed call added nothing, so the size is still 3.

Frequently Asked Questions

add(x) puts exactly one item into the set. update(iterable) loops over one or more iterables and adds every item they contain.
A string is an iterable of characters, so update() adds h, e, l, and o. Use s.add("hello") or s.update(["hello"]) to add the whole word.
Yes. Any number of iterables can be passed in one call, such as s.update(a_list, a_tuple, a_set), and mixed types are fine.
It has the same effect, but s |= other needs another set on the right. update() accepts any iterable and several arguments.
It returns None because it changes the set in place. Use union() when you want a new set and need to keep the original unchanged.

Back to the method index

See all 17 set method guides grouped by what they do.

Method Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful