Python set add

Python set add function is used to add an item to an existing set. In this section, we discuss how to use this add set function. The basic syntax of the Python set add method is

set_Name.add(element)

This Python set add function accepts only one parameter value. However, you can add tuple as an argument. Remember, You can’t add the existing (duplicate value) to a set using this add() method.

Python set add Example 1

The set add function helps you to add a new item to an existing set. Below code add banana to an existing Fruits set.

TIP: Please refer sets article to understand everything about Sets in Python.

FruitSet = {'apple', 'Mango', 'orange', 'cherry','kiwi'}
print("\nOld Set Items = ", FruitSet)

# Set add
FruitSet.add('banana')
print("New Set Items = ", FruitSet)

set add function output


Old Set Items =  {'Mango', 'orange', 'cherry', 'kiwi', 'apple'}
New Set Items =  {'Mango', 'orange', 'cherry', 'kiwi', 'apple', 'banana'}

Let me show you another set add function example. So, you can get a complete idea. This example adds an integer value to an existing integer set.

# Python set add method

IntSet = {10, 20, 30,40}
print("\nOld Set Items = ", IntSet)

# Python Add Item to Set
IntSet.add(125)
print("New Set Items = ", IntSet)

Old Set Items =  {40, 10, 20, 30}
New Set Items =  {40, 10, 20, 125, 30}

Python add tuple to set Example

This example shows how to add tuple to an existing set. Like any other item, you can add tuple using this set add function.

# Python set add method
IntSet = {10, 20, 30,40}
print("\nOld Set Items = ", IntSet)

# Python Add tuple to Set
tupExample = (125, 225, 345)

IntSet.add(tupExample)
print("New Set Items = ", IntSet)
Python set add Function 3

Add Duplicate Set Items Example

This set add example is the same as the above example. However, we are adding the same tuple multiple times. As we mentioned before, tuple doesn’t accept duplicates, and this program adds tuple only one time.

# Python set add method
IntSet = {10, 20, 30,40}
print("\nOld Set Items = ", IntSet)

# Python Add tuple to Set
tupExample = (125, 225, 345)

IntSet.add(tupExample)
print("New Set Items = ", IntSet)

# Python Add tuple to Set
IntSet.add(tupExample)
print("Repeated Adding = ", IntSet)

Adding duple set items output


Old Set Items =  {40, 10, 20, 30}
New Set Items =  {40, 10, (125, 225, 345), 20, 30}
Repeated Adding =  {40, 10, (125, 225, 345), 20, 30}

Python add List to set Example

In this example, we are trying to add the list to an existing set using this set add function.

# Python set add method
IntSet = {10, 25, 30, 40, 50}
print("\nOld Set Items = ", IntSet)

# Python Add List to Set
ListExample = ['apple', 'Orange', 'Grape', 'Mango'] 

IntSet.add(ListExample)
print("New Set Items = ", IntSet)

add list to set output


Old Set Items =  {50, 40, 25, 10, 30}
Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 7, in <module>
    IntSet.add(ListExample)
TypeError: unhashable type: 'list'
>>> 

As you can see from the above output, it is throwing an error because Lists are Mutable. There are other ways to add set items. We explain later.