Python Set add() – Add Element to Set

Spread the love

The set.add() method in Python is used to add an element. The set is an unordered data structure that will not allow duplicates. If we try to add an existing element to the set, then that element won’t be added to the set.

Python sets are classified into two types. Mutable and Immutable. A set created with set() is mutable while the one created with ‘frozenset()‘ is immutable. It allows adding of elements only to the set that was created from the set().

1. Quick Examples of add() Python Set

Below are quick examples of adding elements to a set with different scenarios.


# Create Set
set1=set()

# Add 90 to the set.
set1.add(90)

# Add "hello" to the set.
set1.add("hello")

# Add existing element to the set
set1.add(90)

# Add multiple elements to the set.
set1.add(("hello","sparkby"))

2. Python Set add()

Python set add() method is used to add a single element to an empty set or set containing elements. By using this method you can also add iterables like tuples, lists to the set.

Note that the set.add() is a mutating operation, which means that it modifies the original set in place. If you don’t want to modify the original set, you can create a new set that includes the new element by using the set.union() method. Alternatively, you can also use the set.update() method.

2.1 Syntax of set add()

The following is the syntax of the add() method.


# Set add() syntax:
set.add(iterable/element)

Let’s see some examples to understand this concept better.

3. Add Element to an Empty set in Python

Let’s create a set with a few elements and add an element to the set using add() method in Python. Here is an example.


# Adding element to a set with elements
set1=set({1,2,3,4,5})
print("Set: ",set1)

# Add "hello" to the set.
set1.add("hello")
print("FinalSet: ",set1)

# Output:
# Set:  {1, 2, 3, 4, 5}
# FinalSet:  {1, 2, 3, 4, 5, 'hello'}

Let’s add an element to the empty Set in python. To create an empty set use either set() or {}. Here is an example of how to use add():


# Create empty set
set1=set()
print("Set: ",set1)

# Add 90 to the set.
set1.add(90)
print("FinalSet: ",set1)

# Output:
# Set:  set()
# FinalSet:  {90}

4. Add Element to Set That Already Exists

Let’s create a set with some elements and add elements one by one which already exist.


# Adding element to a set which already exists
set1=set({1,2,3,4,5,"India"})
print("Set: ",set1)

# Add 5 to the set.
set1.add(5)
print("Final Set1: ",set1)

# Add "India" to the set.
set1.add("India")

print("Final Set2: ",set1)

# Output:
# Set:  {1, 2, 3, 4, 5, 'India'}
# Final Set1:  {1, 2, 3, 4, 5, 'India'}
# Final Set2:  {1, 2, 3, 4, 5, 'India'}

Actually, there are 6 elements in the set.

  1. Firstly, we have added 5 to it, as 5 exists in the set, it will not get inserted.
  2. Similarly, “India” is also not inserted.

5. Add Elements from Python Tuple to Set

Let’s create a set with 5 integers and add a tuple having two strings using add() method.


# Adding tuple to a set.
set1=set({1,2,3,4,5})
print("Set: ",set1)

# Add ("hello","sparkby") to the set.
set1.add(("hello","sparkby"))
print("FinalSet: ",set1)

# Output:
# Set:  {1, 2, 3, 4, 5}
# FinalSet:  {1, 2, 3, 4, 5, ('hello', 'sparkby')}

3. Conclusion

We have seen how to add elements to a set using add() method in Python. Note that the set.add() is a mutating operation, which means that it modifies the original set in place. If you don’t want to modify the original set, you can create a new set that includes the new element by using the set.union() method. Alternatively, you can also use the set.update() method.

Leave a Reply

You are currently viewing Python Set add() – Add Element to Set