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
setorfrozensetobject 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 Overflowset.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
setorfrozensetobject 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])
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
Videos
Should I change /usr/bin/python3 to point to Python 3.13?
How do I update Python to 3.13 on Ubuntu 24.04 or 22.04?
Why does python3.13 -m venv say ensurepip is not available?
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)
You'll have to remove the element from the set, and add a new element with that value updated. That's because sets use hashing to efficiently eliminate duplicates. If mutating the elements directly was allowed you'd break this model.
I think you only want the first element to be unique, and track some data associated with that first element. If so you want to use a dictionary instead; use the first element as a key to map to the other two values, in a list for easy altering:
>>> x = {}
>>> x['A1760ulorenaf0821x151031175821564'] = [1, 0]
>>> x['A1760ulorenaf0821x151031175821564'][1] += 1 # increment the second element.
>>> x
{'A1760ulorenaf0821x151031175821564': [1, 1]}
Keys in a dictionary also must be unique.
Note that set.update() only gives you a union operation; the set is updated in-place by adding all elements from the argument(s) that are not already in the set. set.update() cannot alter elements already in the set, because elements the set should not be altered (or at least not in ways that change their hash and equality).
You are better off using a dict if you are trying to have keys and values and you want to update based on key:
x = {'A1760ulorenaf0821x151031175821564' : [1, 0]}
x['A1760ulorenaf0821x151031175821564'] = [1, 1]