What is the difference between add and update operations in python if i just want to add a single value to the set.
a = set() a.update([1]) #works a.add(1) #works a.update([1,2])#works a.add([1,2])#fails
Can someone explain why is this so.
As add() function add a single element to the set, whereas update() function iterates over the given sequences and adds them to the set.
The difference_update() method helps in an in-place way of differentiating the set. The previously discussed set difference() helps to find out the difference between two sets and returns a new set with the difference value, but the difference_update() updates the existing caller set.
Python Set update() Method The update() method updates the current set, by adding items from another set (or any other iterable). If an item is present in both sets, only one appearance of this item will be present in the updated 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).
The short answer is to use the add () of Python to add a single element to the Set variable. You can also update set in Python using the update () of Python. The function is also useful to add multiple elements together to a Set in Python. Let’s find out the use of these two functions below to add or update set in Python.
Parameters : Update () method takes only a single argument. The single argument can be a set, list, tuples or a dictionary. It automatically converts into a set and adds to the set. Return value : This method adds set2 to set1 and returns nothing.
You can also add or append multiple elements to set in Python using the update () function. The function can be used to add single or multiple elements to a set. You can pass single or multiple comma-separated elements within the curly brackets as the argument of the function.
You can add and delete elements from a set, iterate through the set’s elements, and perform standard set operations (union, intersection, difference). Aside from that, you can see if an element is a member of a set.
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
orfrozenset
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With