Introduction to the difference between add and update in Python set

Time:2021-10-27

The set set is a set of unordered non repeating elements

?
1
2
3
4
set(['hello','hello','hi'])
# {'hello', 'hi'}
set('hello hello hi')
# {' ', 'e', 'h', 'i', 'l', 'o'}

The difference between set. Add() and set. Update()

?
1
2
3
4
5
6
7
8
9
myset1 = set()
myset1.add('hello')
#{'hello'}
myset1.update('world')
#{'d', 'hello', 'l', 'o', 'r', 'w'}
myset2 = set()
myset2.add('123')
myset2.update('123')
#{'1', '123', '2', '3'}

Supplement: add vs update in set operation in Python

If I only want to add a single value to the collection, what’s the difference between adding and updating in Python

?
1
2
3
4
5
a = set()
a.update([1]) #works
a.add(1) #works
a.update([1,2])#works
a.add([1,2])#fails

Someone can explain why

resolvent

?
1
set.add

Set.add adds a single element to the collection. Therefore,

?
1
2
3
4
>>> a = set()
>>> a.add(1)
>>> a
set([1])

It works, but it cannot be used with Iterable unless it can be cleared. That’s why A. add ([1,2]) fails

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

Here, [1,2] is regarded as an element added to the collection, and as shown in the error message, a list cannot be hashed, but all elements of the collection should be hashables. Reference documentation,

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

set.update

In the case of set.update, you can pass multiple iterations to it. It will iterate all iterations and include each element in the collection. Remember: it can only accept iterations. That’s why you receive an error when you try to update it with 1

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

However, the following method works because the list [1] is iterated and the elements of the list are added to the collection

?
1
2
3
>>> a.update([1])
>>> a
set([1])

Set.update is basically equivalent to the local set consolidation operation. Consider the following situations:

?
1
2
3
4
>>> 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 iterations into sets, and then we find the union. There are multiple intermediate sets and unions. In this case, set.update can be used as a good help function. Since it accepts any iteratable, you can do it

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

The above is my personal experience. I hope I can give you a reference, and I hope you can support developepper. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]