In this article we will discuss ways to add single or multiple elements to a set in python.

Add a single element to the set

Set in python provides a member function to append a single element to the set i.e.

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. We can use this to add a single value to the set. Let’s understand this with an example,

Adding a value to the set using add() function,

Output:

As 15 was not present in the set, so add() function appended that to the set.

Adding a duplicate element to the set

As set contains only unique elements, so if try to add an element that already exist in the set, then it will have no effect. For example,

Output:

As 15 was already present the set, so add() function did nothing with the set.

Adding immutable objects to the set using add() function

As Sets can only contain immutable elements, therefore we can add int, strings, bytes, frozen sets, tuples, or any other object that is immutable. For example, we can add a tuple to the set using add function,

Output:

Tuple is an immutable object; therefore, it can be added to a set. Whereas list in python is a mutable object, so we cannot add a list to the python using add() function. If you try to pass a list object to the add() function, then it will give error. For example,

Adding a mutable object like list to the set will raise TypeError

Error:

So, we can use the set.add() function to add a single element to the set. What if we want to add multiple elements to the set in one line ?

Adding multiple elements to the set

Set in python provides another function update() to add the elements to the set i.e.

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. We are going to use this update() function to add multiple value to the set in a single line,

Adding multiple values to the set using update() function

Suppose we have a set,

Now we want to add 3 more numbers in the set i.e. 10, 20 and 30. Let’s see how to do that,

Output:

We encapsulated all three elements in a tuple and passed that tuple as an argument to the update() function. Which iterated over all the elements in tuple and added them to the set one by one. Instead of passing a tuple, we can pass a list too i.e.

Output:

update() function will iterated over all the elements in passed list and add them to the set one by one.

Adding elements from multiple sequences to the set

In update() function we can pass multiple iterable sequences and it will add all the elements from all the sequences to the set. Let’s understand by an example,

Suppose we have some elements in different iterable sequences like,

Now we want to add all the elements in above two lists and a tuple to the set. We can do that using update() function,

Output:

We passed both the lists and the tuple to the update() function. It iterated over all of them and added all elements in them to the set.

Let see some more example of update() function,

Adding dictionary keys to the set

Output:

If we pass a dictionary to the update() function of set, then it will try to convert dictionary object to an iterable sequence. Dictionary will be implicitly converted to sequence of keys and update() function will add all the keys in the dictionary to the set.

Adding dictionary values to the set

Output:

We passed a sequence of all values of the dictionary object to the update(). Which added them to the set. As set contains only unique elements, so now it contains only unique values of the dictionary. So, this is how we can add single or multiple elements in the set using add() or update() function.

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.