In this article we will discuss 7 different ways to add all elements of a list to the set in python.

Suppose we have a list and a set i.e.

Now we want to add all the elements of the list to the set. As set contains only unique elements, so after adding elements from a list to the set, the contents of set should be like,

There are different ways to do this and we will discuss them one by one,

Add all elements of a list to set using update() function

In python, the set class provides a member function update() i.e.

It accepts a single or multiple iterable sequences as arguments and adds all the elements in these sequences to the set.

We can use this update() function to add all elements from a list to the set i.e.

Output:

We passed a list as an argument to the update() function. It added all the items in the list to the set. The set contains only unique elements, so items which were not present in the set got added and duplicate items were just skipped.

Adding a list to set using add() function

In python, the set class provides a member function add() i.e.

It accepts a single element as an argument and adds that element to the set. But that element should be immutable.
If we try to pass a list to the add() function, then it will give error because list is a mutable object,

Error

So to add all items in a list to the set using add() function, we need to use a for loop.

Add all items in list to set using add() & for loop

Iterate over all the items in the list using a for loop and pass each item as an argument to the add() function. If that item is not already present in the set,
then it will be added to the set i.e.

Output:

Add a list to set using add() & union()

In python, set class provides a function to add the contents of two sets i.e.

It returns a new set with elements from both s and t.

We can use this to add all elements of a list to the set i.e.

Output:

We converted our list to the set and passed it to the union() function as an argument. union() function returns a set that contains items from both the set i.e. our set and the list (which we converted to the set). As a set contains only unique elements, therefore duplicate elements were just ignored.

Add all elements in a list to set using | operator

We can take union of two sets using | operator too. So, just like the previous solution, we will convert our list to a set and then create a union of both the sets
using | operator i.e.

Output:

Add a list to set using |= and unpacking list to set

Just like the previous solution, we will take a union of two sets. But to convert our list to a set, we will use string literal and unpack our lists elements inside it,

Output:

It added all the items in our list to the set. Now our set contains elements from both the original set and list. Duplicate elements were just skipped.

Adding all elements from multiple lists to the set

Suppose we have 3 different lists,

Now to add all from these lists to the set, we can use update() function,

Output:

In update() function, we can pass multiple iterable sequences as arguments and it adds all the items in these sequences to the set. So, here we passed three lists to the update() function and it added all the elements in these lists to the set.

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.