Python set add

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

setName.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 or duplicate values.

Python set add Example

This function helps you to add a new item to an existing set. The below code inserts a banana into an existing Fruits and prints the same.

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

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

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

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

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


IntSet.add(125)
print("New Items = ", IntSet)

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

TIP: Please refer sets article in Python to understand every detail about them.

In this example, we use this method to add an item to the existing one.

mySet = {1, 2, 4, 5}
print("Old Elements = ", mySet)

mySet.add(3)
print("New Elements = ", mySet)

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

FruitSet.add('banana')
print("New Elements = ", FruitSet)
Old Elements = {1, 2, 4, 5}
New Elements = {1, 2, 3, 4, 5}

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

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 function.

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

tupExample = (125, 225, 345)

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

Add Duplicate Set Items Example

This example is the same as the above example that adds an element to the set. 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.

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

tupExample = (125, 225, 345)

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

IntSet.add(tupExample)
print("Repeated = ", IntSet)

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

In this example, we are trying to add the list to an existing one using this add method.

IntSet = {10, 25, 30, 40, 50}
print("\nOld Items = ", IntSet)

ListExample = ['apple', 'Orange', 'Grape', 'Mango'] 

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

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


Old 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'
>>>