Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add vs update in set operations in python

Tags:

python

set

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.

like image 665
aceminer Avatar asked Mar 04 '15 01:03

aceminer


People also ask

What is the difference between ADD () and update () methods when applied to a set?

As add() function add a single element to the set, whereas update() function iterates over the given sequences and adds them to the set.

What is the difference between the Update () and set () function?

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.

What does set update do in Python?

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.

Can we update an element in a set Python?

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).

How to add or update set in Python?

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.

What is the use of update parameter 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.

How to add or append multiple elements to set in Python?

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.

How do you use a set in Python?

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.


1 Answers

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]) 
like image 196
thefourtheye Avatar answered Oct 13 '22 05:10

thefourtheye