7 Ways to add all elements of list to set in python

How to add all elements of list to set in python ?

As we know a set in python is a built-inĀ  data type which stores multiple unordered items in a single variable.

Example of a set

my_set = {10,20,30,40}

Where,

  • A set contains unique values.
  • All items placed inside { }.

Similarly, a list in python is a built-inĀ  data type which is used to store items of different types.

Example of list

my_list = {30,40,50,60,70}

where,

  • A list contains different types of values integer, float,Ā string etc.
  • All items placed inside [ ].

When we will add all the items of the list to set it will look like this

{10,20,30,40,50,60,70}

Here, all the items are unique after adding as because list contains unique elements.

Method-1 : Add all elements of a list to set using update( ) function

Set class provides an update( ) method with the help of which all elements of list can be added to set.

Syntax : set.update(sequences)
#Program :
    
#Represents a set
my_set = {10, 20, 30, 40}
#Represents a list
my_list = [30, 40, 50, 60, 70, 80]
# adding all elements in list to the set
my_set.update(my_list)
print('After adding list to set : ')
print(my_set)
Output :
After adding list to set : 
{10,20,30,40,50,60,70,80}

In the above program we just passed the list inside the update( ) method, the elements which are not present in set those got added and the elements which are present in set those got skipped as set contains only unique elements.

Method-2 : Add all elements of a list to set using add( ) function

Set class provides an add( ) method with the help of which all elements of list can be added to set.

Syntax : set.add(element)

But the thing is that, add( ) only accept single element inside that. So, if we will pass list in that it will give TypeError: unhashable type: ‘list’ as output.

So to avoid this error we can use for loop and pass one by one element of the list inside add( ) method.

#Program :

#Represents a set 
my_set = {10, 20, 30, 40} 
#Represents a list 
my_list = [30, 40, 50, 60, 70, 80] 
# adding all elements in list to the set 
# By iterating over all elements of list using for loop
for item in my_list:
    # adding each element to the set
    my_set.add(item)
print('After adding list to set : ') 
print(my_set)
Output : 
After adding list to set : 
{10,20,30,40,50,60,70,80}

Method-3 : Add a list to set using add() & union()

We can add content of two sets by using union( ) method.

Syntax : s.union(t)
#Program : 

#Represents a set 
my_set = {10, 20, 30, 40} 
#Represents a list 
my_list = [30, 40, 50, 60, 70, 80] 
#convert list to set
#then add contents two sets 
my_set = my_set.union(set(my_list)) 
print('After adding list to set : ') 
print(my_set)
Output : 
After adding list to set : 
{10,20,30,40,50,60,70,80}

Method-4 : Add all elements in a list to set using | operator

By creating union of 2 sets by using | operator , then we can add elements of two sets. So we have to convert our list to set by using | operator.

#Program :

#Represents a set 
my_set = {10, 20, 30, 40} 
#Represents a list 
my_list = [30, 40, 50, 60, 70, 80] 
# adding all elements in list to the set 
# by converting list to set 
# then get union of both the sets using | operator
my_set  |= set(my_list )

print('After adding list to set : ') 
print(my_set)
Output : 
After adding list to set : 
{10,20,30,40,50,60,70,80}

Method-5 : Add a list to set using |= and unpacking list to set

We will add the elements of list to set just like previous solution means we will convert list to set then we will add the elements by finding union of two sets.

But we will convert list to set by string literal and unpacking our lists elements inside it.

#Program : 

#Represents a set 
my_set = {10, 20, 30, 40} 
#Represents a list 
my_list = [30, 40, 50, 60, 70, 80] 
# adding all elements in list to the set 
# by converting list to set 
# then get union of both the sets using | operator 
# Unpacking elements and OR that with original set
my_set |= set(*my_list ) 
print('After adding list to set : ') 
print(my_set)
Output : 
After adding list to set : 
{10,20,30,40,50,60,70,80}

Add all elements from multiple lists to the set :

Suppose we have 2 different list then add the elements of both the list to set.

#Program : 

#Represents a set 
my_set = {10, 20, 30, 40} 

#Represents a list 
my_list1 = [30, 40, 50, 60, 70] 
my_list2= [80,90] 
# adding all elements in list to the set 
my_set.update(my_list1,my_list2) 
print('After adding list to set : ') 
print(my_set)
Output : 
After adding list to set : 
{10,20,30,40,50,60,70,80,90}