Signup/Sign In
LAST UPDATED: OCTOBER 17, 2022

How to Add a List to a Set in Python?

In this article, we will learn to add a list to a set in Python. We will use some built-in functions and some custom code as well. Let's first have a quick look over what is a list and a set in Python.

Python List

Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets [] to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values.

List Example

list1 = ["Ram", "Arun", "Kiran"]
list2 = [16, 78, 32, 67]
list3 = ["apple", "mango", 16, "cherry", 3.4]

Python Set

Python has a built-in data type called set. It is a collection of unordered data values. An unordered dataset leads to unindexed values. Set values cannot be accessed using index numbers as we did in the list. Set values are immutable which means we cannot alter the values after their creation. Data inside the set can be of any type say, integer, string, or float value. The set uses comma-separated values within curly brackets {} to store data. Sets can be defined using any variable name and then assigning different values to the set in the curly bracket.

Set Example

set1 = {"Ram", "Arun", "Kiran"}
set2 = {16, 78, 32, 67}
set3 = {"apple", "mango", 16, "cherry", 3}

Point to Remember:

Lists are mutable and hence unhashable objects in Python. Whereas, sets in Python are immutable and does not allow unhashable objects. Therefore, Python does not allow a set to store a list. You cannot add a list to a set. A set is an unordered collection of distinct hashable objects. The objects have to be hashable so that finding, adding and removing elements can be done faster than looking at each individual element every time you perform these operations.

So, you cannot add a whole list to a set, instead, you can add elements of that list to a set using different built-in functions. Also, you can add a tuple instead of a list to a set because a tuple is hashable and an immutable object. Let us discuss this with the help of some examples.

Example: Add all Elements of a List to the Set using update() Function

The update() is a built-in function that is used to add elements to a set in Python. This function uses a single line to perform the addition of elements. It is faster and handier than others. This method is useful if the user wants to add multiple items in one go.

The below example takes an input set of elements. A new list of elements is defined that contains elements to add to the original set. We pass the list as an argument to the update() function. It adds all the items of the list to the set. The set contains only unique elements, so items that were not present in the set will get added and duplicate items will skip.

#input set
set1 = {1, 2, 3, 4, 5}

# a list of numbers to add
list_to_add = [6, 7, 8]

# add all elements of list to the set
set1.update(list_to_add)

print('Updated set after adding elements: ', set1)


Updated set after adding elements: {1, 2, 3, 4, 5, 6, 7, 8}

Example: Add all Elements from Multiple Lists to the Set using update() Function

The below example takes an input set of elements. Three new lists of elements are defined that contains elements to add to the original set. We pass the lists as an argument to the update() function. It adds all the items from the three lists to the set. The set contains only unique elements, so items that were not present in the set will get added and duplicate items will skip.

# input set
set1 = {11, 12, 13, 14}

# 3 lists of numbers
list1 = [15, 16, 17]
list2 = [18, 19]
list3 = [30, 31, 19, 17]

# Add multiple lists
set1.update(list1, list2, list3)

#updated list
print('Updated Set: ', set1)


Updated Set: {11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 31}

Example: Add all Elements of a List to the Set using "|" Operator

This example uses "|" operator to add elements to the set. It is similar to the union. We convert the list to a set and then create a union of both sets. But to convert our list to a set, we used the set() function.

#original set
set1 = {1, 2, 3, 4, 5}

#list ofnumbers to add
list1 = [6, 7]

# convert list to set and get union of both the sets using |
set1 |= set(list1)

#updated set
print('Updated Set: ', set1)


Updated Set: {1, 2, 3, 4, 5, 6, 7}

Example: Add all Items of a List using For Loop to a Set

We can use a for loop to add elements into a set with each iteration over all the items in the list and pass each item as an argument to the add() function. The add() adds each element to the set and prints the updated set.

# input set
set1 = {1, 2, 3, 4, 5}

# list of numbers to add
list1 = [6, 7]

# Iterate over all elements of list and
for ele in list1:
        # add each element to the set
        set1.add(ele)

#prints updated set
print('Updated Set after addition: ', set1)


Updated Set after addition: {1, 2, 3, 4, 5, 6, 7}

Example: What happens when you add a list to a set

We can add a list of elements as well into the set. A new list of elements is defined that contains elements to add to the original set. We pass the list as an argument to the add() function. In the case of add() method, if we pass a list to the add() function, then the program will give TypeError because the list has an unhashable object, and add() requires hashable object such as string, tuple, etc.

#input set
set1 = {1, 2, 3, 4, 5}

#list of numbers to add
list1 = [6,7]

# add list to the set
set1.add(list1)

print('Updated set after adding element: ', set1)


TypeError: unhashable type: 'list'

Example: What happens when you Add a tuple to a set

In this example, a tuple of elements is defined that contains elements to add to the original set. We pass the tuple as an argument to the add() function. It adds all the items of the tuple to the set. In this case, if we try to add an already existing tuple to a set then it won’t raise any error because the set takes hashable objects and tuples are hashable.

#input set
set1 = {1, 2, 4, 5}

# tuple to add
tuple1 = (6, 7)

#add tuple to the set 
set1.add(tuple1)

#prints updated set
print("Updated set after adding tuple: ', set1)


Updated set after adding tuple: {1, 2, 4, 5, (6, 7)}

Conclusion

In this article, we learned that we cannot add a list to a set in Python because lists are unhashable objects and sets require only hashable objects. So instead we add elements of a list to set by using built-in functions, update(), and add(). We also used | operator to add elements of a list to the set. We saw TypeError while adding a list to a set. Therefore, use an unhashable object like tuple to add to a set.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.