In this Python tutorial, we’ll learn about Python set add method. We’ll uncover its role and usage in Python set. Practical Python code examples will be used in order for better understanding.
Introduction: Python Set Add Method
This method is used to add items in Python set. It also check that if the item to be added is already present in the set then it’ll not be added.
Let’s now understand it with the help of practical code examples. But first, let’s see its syntax.
Syntax of Add Method
set.add( item )
- This method takes a single argument. This is the item to be added to the existing Python set.
- None is returned by this method.
- Using the add() method while creating a set object will return none. e.g. retVal= set().add(item)
Implementing Python Set Add Method (Multiple Examples)
Below examples will demonstrate the proper usage of add method in Python set.
Example 1: Add Item Using Python Set Add Method
setVal = {1,2,5,7,8} setVal.add( 'Zeeshan' ) print( setVal )
Output
{1, 2, 5, 7, 8, 'Zeeshan'}
We’ve created a simple Python set and have used its add method to add a string item to it. As a result, that item is successfully added to the set. In order to see it visually, we’ve displayed the set using print function.
We can add integer or float as well. See below code:
setVal.add(55) setVal.add(23.6) print(setVal)
Output
{1, 2, 5, 'Zeeshan', 7, 8, 23.6, 55}
Example 2: Add Tuple To Set Using Add Method
We can add Python tuple to set using add method of Python set. See below example:
sVal = {3,5,6} sVal.add( (7,8,'Zee') ) print( sVal )
Output
{(7, 8, 'Zee'), 3, 5, 6}
Example 3: Adding Item that Already Exists in Set
Let’s now try to add an item that is already present in Python set. See below example:
a = {'Zee','Yasir',3,6,7,32.6} a.add('Yasir') print( a )
Output
{32.6, 3, 6, 7, 'Yasir', 'Zee'}
We can see that as the item was already present in the set so it didn’t get added. The result can be seen in the above output block.
Example 4: Add items To Set Using Python For Loop
- We’ll create a simple Python list and specify some items in it.
- After that, we’ll create an empty set. Remember that creating an empty set like this {} will be taken as dictionary by Python. So we’ve to use the set() function.
- Then we will create a for loop and add all the items of list to set using its add method.
- Finally, we’ll display the resulting set. See below code:
lst=['Zeeshan','Yasir','Usman','Saqib','Hasan',34,764,5.8] a=set() for val in lst: a.add(val) print(a)
Output
{34, 'Saqib', 5.8, 'Usman', 'Zeeshan', 'Hasan', 'Yasir', 764}
Example 5: Return Type of Add Method
Let’s now see what our add method return when its used. See below code:
a = {9,3.6,2,9,5} returnedValue = a.add(345) print(a) print(returnedValue)
Output
{2, 3.6, 5, 9, 345} None
We can see that our add method has added the item to set. In order to see what this method returns, we’ve assigned its return value to a variable. Finally, we’ve printed the value of variable using print function and it shows None. It means None is returned by add method of Python set.
So this is how we can properly make use of Python set add method. We hope you now have a detailed understanding of add method of Python set. Don’t hesitate to share it with other Python programmers.
Do ask in the comment section if you still have any questions related to Python set add method. We’ll be glad to answer all.
Conclusion
As a conclusion of this tutorial, we’ve programmatically discussed the usage of Python set add method. We’d be glad to have your informative feedback on it.
You can read our other articles on Python programming for more details on other important Python concepts. Thank you for reading this article.