set.add

set.add adds an individual element to the set. So,

>>> a = set()
>>> a.add(1)
>>> a
set([1])

works, but it cannot work with an iterable, unless it is hashable. That is the reason why a.add([1, 2]) fails.

>>> a.add([1, 2])
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'

Here, [1, 2] is treated as the element being added to the set and as the error message says, a list cannot be hashed but all the elements of a set are expected to be hashables. Quoting the documentation,

Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable.

set.update

In case of set.update, you can pass multiple iterables to it and it will iterate all iterables and will include the individual elements in the set. Remember: It can accept only iterables. That is why you are getting an error when you try to update it with 1

>>> a.update(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: 'int' object is not iterable

But, the following would work because the list [1] is iterated and the elements of the list are added to the set.

>>> a.update([1])
>>> a
set([1])

set.update is basically an equivalent of in-place set union operation. Consider the following cases

>>> set([1, 2]) | set([3, 4]) | set([1, 3])
set([1, 2, 3, 4])
>>> set([1, 2]) | set(range(3, 5)) | set(i for i in range(1, 5) if i % 2 == 1)
set([1, 2, 3, 4])

Here, we explicitly convert all the iterables to sets and then we find the union. There are multiple intermediate sets and unions. In this case, set.update serves as a good helper function. Since it accepts any iterable, you can simply do

>>> a.update([1, 2], range(3, 5), (i for i in range(1, 5) if i % 2 == 1))
>>> a
set([1, 2, 3, 4])
Answer from thefourtheye on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_set_update.asp
Python Set update() Method
Python Tuples Access Tuples Update Tuples Unpack Tuples Loop Tuples Join Tuples Tuple Methods Tuple Exercises Code Challenge Python Sets
Top answer
1 of 9
119

set.add

set.add adds an individual element to the set. So,

>>> a = set()
>>> a.add(1)
>>> a
set([1])

works, but it cannot work with an iterable, unless it is hashable. That is the reason why a.add([1, 2]) fails.

>>> a.add([1, 2])
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'

Here, [1, 2] is treated as the element being added to the set and as the error message says, a list cannot be hashed but all the elements of a set are expected to be hashables. Quoting the documentation,

Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable.

set.update

In case of set.update, you can pass multiple iterables to it and it will iterate all iterables and will include the individual elements in the set. Remember: It can accept only iterables. That is why you are getting an error when you try to update it with 1

>>> a.update(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: 'int' object is not iterable

But, the following would work because the list [1] is iterated and the elements of the list are added to the set.

>>> a.update([1])
>>> a
set([1])

set.update is basically an equivalent of in-place set union operation. Consider the following cases

>>> set([1, 2]) | set([3, 4]) | set([1, 3])
set([1, 2, 3, 4])
>>> set([1, 2]) | set(range(3, 5)) | set(i for i in range(1, 5) if i % 2 == 1)
set([1, 2, 3, 4])

Here, we explicitly convert all the iterables to sets and then we find the union. There are multiple intermediate sets and unions. In this case, set.update serves as a good helper function. Since it accepts any iterable, you can simply do

>>> a.update([1, 2], range(3, 5), (i for i in range(1, 5) if i % 2 == 1))
>>> a
set([1, 2, 3, 4])
2 of 9
27

add is faster for a single element because it is exactly for that purpose, adding a single element:

In [5]: timeit a.update([1])
10000000 loops, best of 3: 191 ns per loop

In [6]: timeit a.add(1) 
10000000 loops, best of 3: 69.9 ns per loop

update expects an iterable or iterables so if you have a single hashable element to add then use add if you have an iterable or iterables of hashable elements to add use update.

s.add(x) add element x to set s

s.update(t) s |= t return set s with elements added from t

People also ask

Should I change /usr/bin/python3 to point to Python 3.13?
No. Ubuntu tools depend on the distro-managed system Python. Keep /usr/bin/python3 pointed to Ubuntu's default interpreter, run Python 3.13 with the versioned binary (python3.13), and use virtual environments for project isolation.
๐ŸŒ
linuxcapable.com
linuxcapable.com โ€บ home โ€บ ubuntu โ€บ how to install python 3.13 on ubuntu (26.04, 24.04, 22.04)
How to Install Python 3.13 on Ubuntu (26.04, 24.04, 22.04) - ...
How do I update Python to 3.13 on Ubuntu 24.04 or 22.04?
Ubuntu 24.04 and 22.04 default to Python 3.12 and 3.10 respectively, so updating means installing Python 3.13 alongside the existing system interpreter. Add the Deadsnakes PPA and install python3.13, or compile from source. The system Python stays unchanged at /usr/bin/python3, and you access 3.13 through the python3.13 binary.
๐ŸŒ
linuxcapable.com
linuxcapable.com โ€บ home โ€บ ubuntu โ€บ how to install python 3.13 on ubuntu (26.04, 24.04, 22.04)
How to Install Python 3.13 on Ubuntu (26.04, 24.04, 22.04) - ...
Why does python3.13 -m venv say ensurepip is not available?
The interpreter is installed, but the venv support package is missing. Install python3.13-venv, then recreate the virtual environment. For source builds, configure Python with --with-ensurepip=install before running make altinstall.
๐ŸŒ
linuxcapable.com
linuxcapable.com โ€บ home โ€บ ubuntu โ€บ how to install python 3.13 on ubuntu (26.04, 24.04, 22.04)
How to Install Python 3.13 on Ubuntu (26.04, 24.04, 22.04) - ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-set-update
Python Set update() - GeeksforGeeks
April 28, 2025 - DSA Python ยท Data Science ยท NumPy ยท Pandas ยท Practice ยท Django ยท Flask ยท Last Updated : 28 Apr, 2025 ยท update() method add multiple elements to a set. It allows you to modify the set in place by adding elements from an iterable (like ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.3 documentation
1 week ago - The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some colle...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ set โ€บ update
Python Set update() (With Examples)
The Python set update() method updates the set, adding items from other iterables. In this tutorial, we will learn about the Python set update() method in detail with the help of examples.
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ sets โ€บ .update()
Python | Sets | .update() | Codecademy
July 2, 2024 - In Python, the .update() method updates the current set by adding items from another iterable, such as a set, list, tuple, or dictionary.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-sets
Python Sets - GeeksforGeeks
To add multiple items we use update() method. ... Because sets are unordered and unindexed, you cannot access elements using a specific index like set[0]. Instead, you must use a loop to iterate through the items or the in keyword to check for ...
Published ย  5 days ago
๐ŸŒ
Medium
medium.com โ€บ @zeebrockeraa โ€บ how-to-use-python-set-update-method-159b5d7bad18
How To Use Python Set Update Method | by Zeeshan Ali | Medium
July 11, 2023 - This method can take one or more arguments. They can be a Python tuples, lists, sets, strings or dictionaries. It does not return anything. It just updates its calling set with the elements of all iterables passed to it as arguments.
๐ŸŒ
LinuxCapable
linuxcapable.com โ€บ home โ€บ ubuntu โ€บ how to install python 3.13 on ubuntu (26.04, 24.04, 22.04)
How to Install Python 3.13 on Ubuntu (26.04, 24.04, 22.04) - LinuxCapable
1 week ago - cat <<'EOF' | sudo tee /usr/local/src/python3.13-build/update-python3.13.sh #!/usr/bin/env bash set -euo pipefail # Verify running as root if [ "$(id -u)" -ne 0 ]; then echo "Run as root: sudo /usr/local/src/python3.13-build/update-python3.13.sh" exit 1 fi # Check required build tools for cmd in curl tar gcc make gpg; do if !
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ python-set-update
Python Set Update - Flexiple
March 15, 2024 - The original set is modified in place. The update() method does not create a new set. The update() method is essential for efficiently managing Python sets. It allows for the flexible addition of multiple elements from various iterables, adhering ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python set update()
Python Set update() - Be on the Right Side of Change
May 1, 2021 - You can update an existing set with all elements in a given list by calling set.update(list). This will insert all elements from the list in the set. As the set data structure is duplicate-free, all duplicate entries will be removed.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ python โ€บ python set update method
Python Set Update Method
February 21, 2009 - The Python Set update() method is used with sets to modify the set by adding elements from another iterable or set. It takes an iterable such as another set, list or tuple as an argument and adds its elements to the calling set.
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-set-update-method
Python Set update() Method - Learn By Example
April 20, 2020 - The update() method updates the original set by adding items from all the specified sets, with no duplicates. You can specify as many sets as you want.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard-library โ€บ set โ€บ update
Python set update() - Merge Sets
December 31, 2024 - This code reaffirms that once list1 has been used to update set1, further updates with the same list donโ€™t make any changes to set1. Both print statements output the same result, {1, 2, 3, 4}. The update() method in Python sets is invaluable for merging multiple sets or other iterables into a single set efficiently, maintaining uniqueness of elements without duplications.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ need help understanding differences in sets (update vs union)
r/learnpython on Reddit: Need help understanding differences in sets (update vs union)
October 17, 2021 -

So I have recently started to learn python, it was going well until I started getting into sets.

What is the difference between object.update(object) vs (object)=object.union(object)

I tried testing both but they print out the same thing so Iโ€™m confused why I would use one vs the other.

My code Iโ€™m using to test:

a = {'1','2','3','4','5','6'} b = {'4','5','6','7','8','9'}

print('a: '+str(a)) print('b: '+str(b))

a.update(b)

print('a updated with b: '+str(a))

c = a.union(b)

print('a unionized with b: '+str(c))

Iโ€™m probably doing it wrong so any help is appreciated. (Please use simple terms, I tried looking online but I ended up more confused)

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-update-set-items
Python Update Set Items - GeeksforGeeks
July 23, 2025 - Python sets are a powerful data structure for storing unique, unordered items. While sets do not support direct item updating due to their unordered nature, there are several methods to update the contents of a set by adding or removing items.
๐ŸŒ
docs.python.org
docs.python.org โ€บ 3 โ€บ library โ€บ sets.html
Built-in Types โ€” Python 3.7.3 documentation
The C implementation of Python makes the list appear empty for the duration, and raises ValueError if it can detect that the list has been mutated during a sort. Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the enumerate() built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set ...