Python Set: add() vs update()

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:

set.add(element)

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:

set.update(*args)

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.

sample_set = {"Hi", "This", "is", 4, 3, 3}

# Add only a single element in set
sample_set.add(10)

print(sample_set)

Output:

{'Hi', 3, 4, 10, 'This', 'is'}

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,

sample_set = {"Hi", "This", "is", 4, 3, 3}

# Adding multiple elements to the set
sample_set.update([11, 12, 13, 14])

print(sample_set)

Output:

{'is', 3, 'This', 4, 'Hi', 11, 12, 13, 14}

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,

sample_set = {"Hi", "This", "is", 4, 3, 3}

# Passing a mutable list object to the add() function
# It will give error
sample_set.add([11, 12, 13, 14])

Error:

TypeError: unhashable type: 'list'

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,

sample_set = {"Hi", "This", "is", 4, 3, 3}

# Passing a list to update() will add all elements in list to the set
sample_set.update([11, 12, 13, 14])

print(sample_set)

Output:

{'is', 3, 'This', 4, 'Hi', 11, 12, 13, 14}

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

sample_set.update(55)

Error:

TypeError: 'int' object is not iterable

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

sample_set = {"Hi", "This", "is", 4, 3, 3}

# passing multiple sequences to the update() function
sample_set.update([11, 12], (21, 22), [31, 32])

print(sample_set)

Output:

{32, 'is', 3, 'This', 4, 'Hi', 11, 12, 21, 22, 31}

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,

def main():

    sample_set = {"Hi", "This", "is", 4, 3, 3}

    print('Original Set:')
    print(sample_set)

    print(' **** Differences between add() & update() functions of set ****')

    print('*** Difference 1: Number of elements to be added ***')

    print('Add an element in set using add() function')

    # Add only a single element in set
    sample_set.add(10)

    print('Modified Set:')
    print(sample_set)

    print('Add multiple element in set using update() function')

    sample_set = {"Hi", "This", "is", 4, 3, 3}

    # Adding multiple elements to the set
    sample_set.update([11, 12, 13, 14])

    print('Modified Set:')
    print(sample_set)


    print('*** Difference 3: Mutable and immutable parameters *** ')

    sample_set = {"Hi", "This", "is", 4, 3, 3}

    print('Passing a mutable object to add() will give error')

    # Passing a mutable list object to the add() function
    # It will give error => TypeError: unhashable type: 'list'
    # sample_set.add([11, 12, 13, 14])

    print('Passing a mutable object like list to update() function')

    # Passing a list to update() will add all elements in list to the set
    sample_set.update([11, 12, 13, 14])

    print('Modified Set:')
    print(sample_set)

    print('Passing anything other than iterable sequence to the update() function will give error')

    # As 55 is not iterable sequence, so it will give error
    # Error => TypeError: 'int' object is not iterable
    #sample_set.update(55)

    print('*** Difference 4: Passing multiple arguments ***')

    sample_set = {"Hi", "This", "is", 4, 3, 3}

    # passing multiple sequences to the update() function
    sample_set.update([11, 12], (21, 22), [31, 32])

    print('Set contents: ')
    print(sample_set)


if __name__ == '__main__':
   main()

Output:

Original Set:
{3, 4, 'is', 'This', 'Hi'}
 **** Differences between add() & update() functions of set ****
*** Difference 1: Number of elements to be added ***
Add an element in set using add() function
Modified Set:
{3, 4, 10, 'is', 'This', 'Hi'}
Add multiple element in set using update() function
Modified Set:
{3, 4, 11, 12, 13, 14, 'is', 'This', 'Hi'}
*** Difference 3: Mutable and immutable parameters *** 
Passing a mutable object to add() will give error
Passing a mutable object like list to update() function
Modified Set:
{3, 4, 11, 12, 13, 14, 'is', 'This', 'Hi'}
Passing anything other than iterable sequence to the update() function will give error
*** Difference 4: Passing multiple arguments ***
Set contents: 
{32, 3, 4, 11, 12, 'is', 21, 22, 'This', 31, 'Hi'}

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top