Python set add

The 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 a tuple as an argument. Remember, You can’t insert 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 Python set add function. So, you can get a complete idea. This example adds an integer value of 125 to 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 to the sets article in this Programming language 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'}

How to add a tuple to the Python set?

This example shows how to add a tuple to an existing set. Like any other item, you can add a 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

Duplicates Example

This Python example is the same as the above, which will add the tuple element to the set. However, we are adding the same tuple multiple times. As we mentioned, a tuple doesn’t accept duplicates, and this program only adds a tuple once.

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 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 from the output, it is throwing an error because Lists are Mutable. There are other ways to insert items. We will 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'
>>>