Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

How to Add Elements in a Set

In this article, we will learn to add elements in a set in Python. We will use some built-in functions, some simple approaches, and some custom codes as well to better understand the topic. Let's first have a quick look over what is a set in Python.

Set

Python has a built-in data type called set. It is a collection of unordered data values. Due to the unordered dataset, the user is not sure of the order in which data values appear. 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. For example,

Set Example

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

Add elements in a Set

Adding elements to a set in Python basically means updating the set with one or multiple elements. We know that set values are immutable that means the values of a set cannot be changed after its creation. However, the set itself is mutable which means we can perform add, read, and delete operations on it. In this article, we will learn to add one or more elements and observe the following outputs.

Let's look at the below methods and learn what are the different ways to add elements to a given set.

  1. Using update() function
  2. Using add() function
  3. Using union() function
  4. Using "|" operator

1. Using update() function

This is a built-in function to which 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.

Syntax

set.update( iterable )

Example: Add all elements of a list to the set

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

The below example takes an input set of elements. Three new list 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}

2. Using add() function

This is a built-in function of set which is used to add an element to the set. This function adds only one item at a time to the set. The set contains only unique elements, so if we try to add an element that already exists in the set, it does not add that element and runs the program with the original set.

Syntax

set.add( element )

Example: Add an element to a set

In this below example, we passed an element '6' as an argument to the add() function. It adds '6' to the set and prints the updated set.

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

# add an element to the set
set1.add(6)

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


Updated set after adding element: {1, 2, 3, 4, 5, 6}

Example: Add a list to a set

We can add a list of elements as well into set. A new list of elements is defined that contains elements to add into the original set. We pass the list as an argument to the add() function. In case of add() method, if we pass a list to the add() function, then the program will give TypeError because the list has 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: 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 add it and it won’t raise an error.

#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)}

Example: Add all items of a list using for loop to a set

We can use a for loop to add elements into 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 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}

3. Using union() function

Set provides another function to add the contents of two sets in one set using union() method. We can use this method to add all elements of an iterable to the set. We convert our iterable to the set and passed it to the union() function as an argument. As a set contains only unique elements, therefore duplicate elements will be ignored.

Syntax

set1.union(iterable)

Example: Add elements using union() function

In this example, we added two sets elements by using the union function and get a new set that combinly has all the elements of both the set.

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

#iterable = set
set2 = {6, 7}

#convert list to set and get union of both the sets
set1 = set1.union(set(set2))

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


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

4. 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 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}

Difference between add() and update() method

  • add() method adds a single item when used alone while update() method adds multiple items.
  • add() method expects a single hashable type object while update() method expects an iterable object.

Conclusion

In this article, we learned to add elements to a set by using three built-in functions, update(), union() and add(). We also used | operator to add elements to the set. We used some custom codes as well to understand different problems with the add() function. We learned the difference between add() and update() function. We saw several examples of addition and saw what happens when different iterables (tuple, list, set) are added to the set.