The set
is one of the core data types in Python, it represents a collection of non-ordered, unique elements. Just like lists, sets are mutable, meaning that we can perform operations on them in-place.
The set.update()
method is used to add multiple distinct elements into a set. Its syntax is as shown below:
set.update(iterable)
iterable |
An iterable containing the elements to be added to the set. We can also pass multiple iterables. |
The function adds distinct elements from the iterable into the set. It returns None
.
Consider the following example:
In the above example we updated the sets using elements from a list, we can also use any other iterable such as tuples, dictionaries, range or even other sets. In the following example we pass most iterables as the argument to the update()
function.
Since sets can only contain unique elements, if the iterable given as the argument has an element that is already in the set; that element will be ignored.