Register Login

Different ways of adding Values to a Set in Python

Updated Apr 28, 2022

Python consists of sets that are responsible for storing unique elements or objects in an unordered fashion. Tuple or Lists might contain duplicate values. However, we cannot add identical values to Python sets.

This is a special data type of Python and programmers need to know how to add values with sets. This article will guide you on how you can add elements to a Python set.

What are the Different Ways of Adding Values to a Set in Python?

Consider a list and a set.

# Set
sample_set = {2, 3, 4, 5}
# List
list_of_num = [1, 2, 3, 4, 5, 6]

Now, we want to add every element to the unique set. So, after we add the list into it, it will look like this:

{1, 2, 3, 4, 5, 6}

There are many methods using which you can add values to a set.

Using update() function

Syntax

set.update(sequences)

The update() function accepts the iterable sequences as arguments. Then they pick all the elements from the sequence and add them to the set.

Program:

sample_set = {2, 3, 4, 5}
list_of_num = [1, 2, 3, 4, 5, 6]
sample_set.update(list_of_num)
print('Modified Set: ')
print(sample_set) 

Output:

The update() function added the list to the set. The duplicate or repetitive elements got skipped.

Using add() function

Syntax:

set.add(element)

The add() function accepts a single sequence as an argument. It then adds that element to the set. However, it should be immutable. We know that a list is mutable. So, if we pass it using the add() function, it will show an error.

Example:

sample_set.add(list_of_num)

Output:

TypeError: unhashable type: 'list'
To eliminate the error, we have to use for loop along with the add() function.

Using add() function along with for loop

For loop iterates the list elements. It then passes every item to the add() function as an argument. Then they are added. If the set does not contain the elements present in the list, add() function will join it.

Program:

sample_set = {2, 3, 4, 5}
list_of_num = [1, 2, 3, 4, 5, 6]
for elem in list_of_num:
  sample_set.add(elem)
print('Modified Set: ')
print(sample_set)

Output:

Apart from the 'for loop', we can also use the union() function with add().

Using add() and union()

Syntax:

s.union(t)

This function returns a set that constitutes values of both s and t.

Program:

sample_set = {2, 3, 4, 5}
list_of_num = [1, 2, 3, 4, 5, 6]
sample_set = sample_set.union(set(list_of_num))
print('Modified Set: ')
print(sample_set)

Output:

The function ignored the duplicate elements. Union() combined the items of the list and the set.

Using | operator

You can combine the list and the set by using the | operator. We will follow almost the same steps we followed previously.
We will convert the list into a set by combining them.

Program:

sample_set = {2, 3, 4, 5}
list_of_num = [1, 2, 3, 4, 5, 6]
sample_set |= set(list_of_num)
print('Modified Set: ')
print(sample_set)

Output:

Using |= and unpacking list to set

In this function, we will take the union of both sets. However, to convert the list, we use a 'string literal. In the next step, we unpack the elements of the list.

Program:

sample_set = {2, 3, 4, 5}
list_of_num = [1, 2, 3, 4, 5, 6]
sample_set |= {*list_of_num}
print('Modified Set: ')
print(sample_set)

Output:

The final output contains elements from the original set and the list. The function skipped the duplicate items.

Adding elements from different lists to the set

Let us consider three lists.

list_num_1 = [15, 16, 17]
list_num_2 = [18, 19]
list_num_3 = [30, 31, 19, 17]

We can use update() function to add these.

Program:

sample_set = {11, 12, 13, 14}
sample_set.update(list_num_1, list_num_2, list_num_3)
print('Modified Set: ')
print(sample_set)

Output:

Adding Objects to a Set

We add individual elements using add() function. In the same way, we can add objects. The only condition is that the objects should be immutable. For instance, we can add a tuple to a list. However, we cannot add a list to a particular set because lists are immutable. It will show an error.

Program:

mySet = set([1, 2, 3, 4, 5]) 
print("Original Set is:", mySet) 
myTuple = (6, 7, 8) 
print("List of values is:", myTuple) 
mySet.add(myTuple) 
print("Set after updating :", mySet)

Output:

Conclusion

Among all of these, the update and add are the easiest functions we can use to add values to a set in Python. Apart from that, we also saw various other techniques for adding values to a set in this article. We also discussed how we should use for loop with add() to take multiple elements from a list and convert them to set elements.


×