In this article we will discuss the main differences between add() and update() functions of Set in python.

In python, set class provides two different functions to add or append elements in the set. Before going into the differences, first let’s have a basic overview about them,

set.add() Function:

It accepts an element as an argument and if that element is not already present in the set, then it adds that to the set. It returns nothing i.e. None.

set.update() Function:

It expects a single or multiple iterable sequences as arguments and appends all the elements in these iterable sequences to the set. It returns nothing i.e. None.

Now we will focus on differences between them,

Differences between add() and update()

  1. Use add() function to add a single element. Whereas use update() function to add multiple elements to the set.
  2. add() is faster than update().
  3. add () accepts immutable parameters only. Whereas accepts iterable sequences.
  4. add() accepts a single parameter, whereas update() can accept multiple sequences.

Now we will discuss each of them in detail

Difference 1: Number of elements to be added

Using add() function, we can add only a single element to the set i.e.

Output:

We passed a value 10 to the add() function, as it was not present in the set, so add() function added that to the set.

Whereas can use update() function to add multiple elements to the set in a single line,

Output:

Here we passed a list object as an argument to the update() function and it iterated over all the elements in that list and added them to the set one by one.

Difference 2: add() is faster than update()

As add() function add a single element to the set, whereas update() function iterates over the given sequences and adds them to the set. Therefore, as compared to update() function, add() is better in performance.

Difference 3: Mutable and immutable parameters

add() function accepts an immutable argument i.e. we can pass int, strings, bytes, frozen sets, tuples or any other immutable object to the add() function.

So if we try to pass a mutable object like list to the add() function, then it will give error,

Error:

Whereas update() function expects iterable sequences only. For example if we pass a list to the update() function, then it will add all the elements in list to the set,

Output:

If we pass anything other than iterable sequence to the update() function, then it will give error,

Error:

Here we passed an integer to the update() function, but it accepts only iterable sequences. Therefore, it gave the error.

Difference 4: Passing multiple arguments

While calling add() function, we can pass only one argument and it will add that element to the set. Whereas, while calling update() function, we can pass multiple arguments i.e. multiple iterable sequences

Output:

update() function will add all the elements in all sequences to the set.

So, these were the 4 main differences between update() and add() functions of set in python.

The complete example is as follows,

Output:

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.