ReferenceSet methods

Python Set update() Method

Short answer
set.update(*iterables) adds every item of each iterable into the set in place and returns None. Duplicates are ignored.

Use update() when you already have a collection of values to add: new tags from a list, extra cities from another set, or the characters of a string. It is the multi-item version of add().

It changes the set you call it on. If you want to keep the original and get a new set instead, use union(). Remember that strings are iterables, so update("abc") adds the single characters, which surprises many beginners.

Quick facts
Syntaxmy_set.update(*iterables) or my_set |= other_set
ParametersZero or more iterables
ReturnsNone (the set is changed in place)
Time complexityO(total number of items added)

How set.update() works

Python loops through each iterable and adds every item, exactly like calling add() repeatedly. Items already present are skipped.

Python
tags = {"python"}
tags.update(["sets", "python"], ("loops",), {"dicts"})
print(sorted(tags))

update() with strings and other iterables

Because every iterable is unpacked, a string contributes its characters, and a dictionary contributes its keys. To add a whole word, wrap it in a list or use add().

Python
letters = set()
letters.update("cab")
print(sorted(letters))
words = set()
words.update(["cab"])
print(words)

update() vs add() vs union() vs |=

ToolAddsChanges the set?Returns
add(x)One element, kept wholeYesNone
update(it)Every item of the iterableYesNone
union(it)Every item, in a copyNoNew set
s |= otherItems of another setYesThe same set
s.update(a, b)Items of several iterablesYesNone

Examples you can run

Press Run, then edit the code and run it again.

Merge new tags into a post

The set grows only by tags it did not have yet.

Merge new tags into a post
post_tags = {"python", "sets"}
new_tags = ["sets", "tips", "tutorial"]
post_tags.update(new_tags)
print(sorted(post_tags))
print(len(post_tags))
Expected output['python', 'sets', 'tips', 'tutorial']
4

Characters of a string

Collect the unique letters of a word.

Characters of a string
seen = set()
seen.update("banana")
print(sorted(seen))
Expected output['a', 'b', 'n']

Common mistakes

Passing a single number

✗ Wrong

s = {1, 2}
s.update(3)
TypeError: 'int' object is not iterable

✓ Right

s = {1, 2}
s.add(3)
print(sorted(s))

update() needs an iterable. Use add(3) for one element or update([3]).

Using the return value

✗ Wrong

s = {1}
s = s.update([2, 3])
print(s)
None

✓ Right

s = {1}
s.update([2, 3])
print(sorted(s))

update() returns None. Call it on its own line.

Python Set update() Method: questions people ask

What is the difference between add() and update() in a Python set?

add() inserts one element, while update() inserts every item of one or more iterables. Passing a string to update() adds its characters.

Does set.update() create a new set?

No. It modifies the set in place and returns None. Use union() for a new set.

Can update() take a list or a dictionary?

Yes, any iterable works. A list adds its items and a dictionary adds its keys.

Keep learning

Step-by-step lessons:
Python SetsPython Set Operations

Related:
Python Set add() MethodPython Set union() MethodPython Set clear() MethodPython Set remove() Method

More in set methods:
Python Set add() MethodPython Set remove() MethodPython Set discard() MethodPython Set pop() MethodPython Set union() MethodPython Set intersection() MethodPython Set difference() MethodPython Set symmetric_difference() Method See all →