Set update python – Python Set: add() vs update()

Set update python: A set in Python is a data structure that is analogous to a set in mathematics. It can have any number of elements; the order of the elements in a set is undefined. 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.

In contrast to arrays, where the elements are stored in an ordered list, the order of elements in a set is undefined (moreover, the set elements are usually not stored in order of appearance in the set; this allows checking if an element belongs to a set faster than just going through all the elements of the set).

Add vs Update in Set

Python set add element: In this article, we will look at the main differences between Python’s add() and update() functions for Set.

1)add() function

Add element to set python: If the element is not already present in the set, the set add() method adds it.

Syntax:

set.add(element)

Parameters:

add() accepts a single parameter (element) that must be added to the set.

Return:

The add() method does not return any value.

2)update() function

Add elements to set python: set’s update() function adds elements from another set (passed as an argument) to the set.

Syntax:

set.update(*args)

Parameters:

The Update() method only accepts one argument. A set, list, tuples, or dictionary can be used as the single argument. It converts and adds to the set automatically.

Return:

This method simply adds args to set and returns null.

3) add() vs update() difference

  • To add a single element, use the add() function. To add multiple elements to a set, use the update() function.
  • add() is quicker than update().
  • Only immutable parameters are accepted by add (). In contrast, accepts iterable sequences.
  • add() only takes one parameter, whereas update() can take multiple sequences.

We will discuss this one by one

4)Difference #1:Number of items to be added to set

i)adding single element using add()

Adding to a set python: We can only add one element to the set using the add() function.

Below is the implementation:

# given set
givenset = {'hello', 'this', 'is', 'BTeechGeeks'}
# adding element to set using add()
givenset.add('Python')
# print the set
print(givenset)

Output:

{'hello', 'BTeechGeeks', 'this', 'is', 'Python'}

ii)adding multiple elements using update()

How to add to a set: In contrast, the update() function can be used to add multiple elements to the set in a single line.

Below is the implementation:

# given set
givenset = {'hello', 'this', 'is', 'BTeechGeeks'}
# adding elements to set using update()
givenset.update(['Python', '2021', 'platform'])
# print the set
print(givenset)

Output:

{'2021', 'platform', 'is', 'BTeechGeeks', 'this', 'Python', 'hello'}

5)Difference #2:Execution time

Python set add set: Add() adds a single element to the set, whereas update() iterates over the given sequences and adds them to the set. As a result, add() outperforms the update() function in terms of performance.

6)Difference #3: Mutable and immutable parameters in add() and update() function

i)add() function

How to add elements to a set in python: The add() function accepts immutable arguments, which means that we can pass int, strings, bytes, frozen sets, tuples, or any other immutable object to it.

So, if we try to pass a mutable object, such as a list, to the add() function, we will get an error.

Below is the implementation:

# given set
givenset = {'hello', 'this', 'is', 'BTeechGeeks'}
# adding elements to set using add()
givenset.add(['Python', '2021', 'platform'])
# print the set
print(givenset)

Output:

Traceback (most recent call last):
  File "/home/520c50fb7c3daaa8db99bf168af58f53.py", line 4, in <module>
    givenset.add(['Python', '2021', 'platform'])
TypeError: unhashable type: 'list'

ii)update() function

Add items to set python: The update() function only accepts iterable sequences. If we pass a list to the update() function, it will add all of the elements in the list to the set.

Below is the implementation:

# given set
givenset = {'hello', 'this', 'is', 'BTeechGeeks'}
# adding elements to set using update()
givenset.update(['Python', '2021', 'platform'])
# print the set
print(givenset)

Output:

{'2021', 'platform', 'is', 'BTeechGeeks', 'this', 'Python', 'hello'}

7)Difference #4:Adding multiple elements using update

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

Below is the implementation:

# given set
givenset = {'hello', 'this', 'is', 'BTeechGeeks'}
# adding elements to set using update()
givenset.update(['Python', '2021'], ['platform', 'online'], ('India', 122))
# print the set
print(givenset)

Output:

{'hello', 'is', '2021', 'platform', 'online', 122, 'India', 'Python', 'this', 'BTeechGeeks'}

Related Programs: