Python set add() method Explained [Easy Examples]

 

Introduction to Python set add() method

A set in Python is an unordered collection of different data types that is iterable, mutable, and does not contain any duplicate elements. The order of elements in a python set is not defined. The advantage of using python set is that we can apply different mathematical operations which help us to analyze and process the sets. Today in this tutorial, we will learn about the python set add() method and see how we can add different data typed elements in a given set. We will also take different cases including taking tuples, numeric values, and strings, and will see how we can add them to the set using the python set add() method. In a nutshell. this tutorial contains all the information that you need to know in order to start working with the python set add() method.

 

Getting started with the python set add() method

Set is a data structure that stores unique elements in it. The python set add() method is a built-in function of a set data structure that takes an element and adds it to the set. In this section, we will see the simple syntax of the python set add() method with some real examples.

Advertisement

 

Syntax of Python set add() method

The simple syntax of python set add() is very simple. It looks like this:

set_name.add("element_name")

The python set add() method takes an argument that is to be added to the list. This argument can be integer, string, or a tuple depending on the situation. Now notice that the python set add() method does not returns any value or data type, it just adds the value to the already existing set. See the example below:

# python set
set1 ={1, 4, 5,3}

# printing the type of python add set()
print(type(set1.add(6)))

Output:

<class 'NoneType'>

Note that the return type is None which means it does not return any value. If we try to print it we will get None. See the example below:

# python set
set1 ={1, 4, 5,3}

# printing python add set()
print(set1.add(6))

Output:

None

 

Example-1: Adding string characters to set using python set add() method

Know we are familiar with the syntax and basics of the python set add() method. Let us now apply it to the real problem to add a new element type of string to the set. See the example below:

Advertisement
# python string typed set
set1 ={"a", "b", "c", "d"}

# printing set
print("Before adding new element: {}".format(set1))

# adding element to set
set1.add("z)

# printing set after adding element
print("After  adding new element: {}".format(set1))

Output:

Before adding new element: {'c', 'd', 'b', 'a'}
After adding new element: {'z', 'c', 'a', 'd', 'b'}

Notice that we were able to add a new element to the existing set.  As we already discussed the python set add() method can only take one argument. If we try to add multiple elements at once and passed them as an argument, we will get an error. See the example below:

# python string typed set
set1 ={"a", "b", "c", "d"}

# adding elements to set
set1.add("z", "e", "t")

# printing set after adding element
print("After  adding new element: {}".format(set1))

Output:

Python set add() method Explained [Easy Examples]

Note that we get an error that says add() method only takes one argument. If we want to add multiple elements to the set, we can do that by using for loop and iterating over the elements and add them to the existing set. See the example below:

# python string typed set
set1 ={"a", "b", "c", "d"}

# set of elements to be added
set2 = {"e", "f", "g", "i"}

# for loop
for element in set2:

   # python set add() method
   set1.add(element)

# printing set after adding element
print("After  adding new elements: {}".format(set1))

Output:

After adding new elements: {'f', 'b', 'e', 'c', 'i', 'g', 'a', 'd'}

Finally, we were able to add multiple elements in a set using for loop.

 

Example-2: Adding numeric values to set using python set add() method

In a similar way, we can also add numeric values to the set as well. A set can contain any kind of elements so we can easily add numeric values. See the following example which adds numeric values.

# python string typed set
set1 ={"a", "b", "c", "d"}

# adding numeric value
set1.add(3)

# printing set after adding element
print("After  adding new elements: {}".format(set1))

Output:

Advertisement
After adding new elements: {3, 'c', 'a', 'b', 'd'}

We can also add multiple numeric values using for loop as we did for string characters. See the example below which adds multiple numeric values to the set:

# python string typed set
set1 ={"a", "b", "c", "d"}

# set of elements to be added
set2 = {1, 2, 3, 4, 5, 6}

# for loop
for element in set2:

   # python set add() method
   set1.add(element)

# printing set after adding element
print("After  adding new elements: {}".format(set1))

Output:

After adding new elements: {1, 2, 3, 4, 5, 6, 'd', 'a', 'c', 'b'}

We can use the same method to add values other than numeric ones.

 

Example-3: Adding tuple to set using python set add() method

Tuples are used to store multiple items in a single variable. In this section, we will add a tuple to set using the python set add() method. See the example below:

# python string typed set
set1 ={"a", "b", "c", "d"}

# tuple
tup = ("e", "f", "g")

# adding tuple value
set1.add(tup)

# printing set after adding element
print("After  adding a tuple: {}".format(set1))

Output:

After adding a tuple: {'b', 'a', ('e', 'f', 'g'), 'c', 'd'}

Notice that we added a tuple to the given set. If we want to add the elements of a tuple individually, not as a tuple, we can do that by using a for loop and iterating over a tuple. See the following example.

# python string typed set
set1 ={"a", "b", "c", "d"}

# set of elements to be added
tup = ("e", "f", "g")

# for loop
for element in tup:

   # python set add() method
   set1.add(element)

# printing set after adding element
print("After  adding new elements: {}".format(set1))

Output:

After adding new elements: {'f', 'a', 'b', 'e', 'd', 'g', 'c'}

 

Example-4: Adding lists to set using python set add() method

A list is any information displayed or organized in a logical or linear formation. We can't add a list to a set because lists are mutable, meaning that we can change the contents of the list after adding it to the set. If we try to add it, we will get an error. See the example below:

Advertisement
# python string typed set
set1 ={"a", "b", "c", "d"}

# tuple
list1 = ["e", "f", "g"]

# adding list
set1.add(list1)

# printing set after adding element
print("After  adding a list: {}".format(set1))

Output:

Python set add() method Explained [Easy Examples]

But we can add the items to the list individually by using for loop and iterating over list. See the example below:

# python string typed set
set1 ={"a", "b", "c", "d"}

# set of elements to be added
list1 = ["e", "f", "g"]

# for loop
for element in list1:

   # python set add() method
   set1.add(element)

# printing set after adding element
print("After  adding new elements: {}".format(set1))

Output:

After adding new elements: {'e', 'f', 'd', 'c', 'b', 'g', 'a'}

Adding elements individually is allowed but as a list is not allowed in sets.

 

Example-5: Adding dictionary to set using python set add() method

A dictionary in Python is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. Like lists, we cannot directly add a dictionary to the set but we can iterate over the dictionary and add keys to the set. See the example below which iterates over dictionary using for loop and adds keys and values to the set.

# python string typed set
set1 ={"a", "b", "c", "d"}

# set of elements to be added
dic ={"bashir":2, "k":98, "pak":1947}

# for loop
for element in dic:

   # python set add() method
   set1.add(element)

# printing set after adding element
print("After  adding new elements: {}".format(set1))

Output:

After adding new elements: {'a', 'k', 'pak', 'd', 'b', 'bashir', 'c'}

Notice that the elements are arranged in an unordered because the order does not matter in sets.

 

Summary

Sets are used to store multiple items in a single variable. In Python, we can add elements using a built-in function, the python set method().  In this tutorial, we learned about the python set add() function and came across various cases of adding different data types to the set. We learned how we can add string, numeric values, and tuples to the set. Moreover, we also discussed why we cannot add a list and dictionary to the set and how we can iterate over them to add elements All in all this tutorial, covers all the details that you need to know about the python set add method.

 

Further Reading

Sets in python
python set
python data structure

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment