In this post, We are going to learn how to Python Set add single or multiple elements with examples. We are going to use Python Set built-in methods add() and update() to add elements in Set.
Python Set add single or multiple elements
- Add() method: This method is used to add a single element to Python Set
- Update() method: This method is used to Add Multiple elements to a Set. it takes a list, tuple, or dictionary to set as an argument
1.Python Set add() method
Python Set add() method is used to add a single element in the set. If the element is already existing in the set then it will not add the element Else passed element will be added to the set. It is used to add a new element.
Syntax
set.add(element)
Element: It is the required parameter, and represents the element we are adding in SET.
Return Value
The python set add() method does not return any value.
1.2 Python Set Add single element
Here we are adding two elements ‘Good’ and 1. The ‘Good’ will be added to the set. But element 1 already exists. So it will not be added in SET again. because Python set always contains unique elements.
Program Example :
orignal_set = {'evening',1}
#adding element using Set add() method
orignal_set.add('Good')
#adding element that already exists in set
orignal_set.add(1)
print('set after adding element :',orignal_set)
Output
set after adding element :
{1, 'evening', 'Good'}
1.3 Python Set Add a tuple
We can add a tuple to the python set In this example, we are adding a python tuple using add() method.
Program Example :
orignal_set = {'evening',1}
#using Set add() method adding tuple
my_tuple = ('a',65,'b',67)
orignal_set.add(my_tuple)
print('set after adding tuple :',orignal_set)
Output
set after adding tuple : {1, ('a', 65, 'b', 67), 'evening'}
2. Update() to Add multiple element to Set
To add multiple elements at once we use the Set update() method. It takes an iterable(list, tuple, dictionary) as an argument. We can add single or multiply iterable in the set using the Update() method.
Syntax
set.update(iterable1....iterablen)
iterable: This is a required parameter. It can be one or multiple iterables. These include string, list, dictionary, tuple. If we are passing single iterable or multiple iterable elements of iterable will get added to SET.
Return Value
Python set update() method does not return any value.
2.1. Python set Add a list
Now let us understand this with the help of an example. Python set update() method can add single or multiply iterable(list,tuple,dictionary) to set. Here we are adding a single iterable(list).
list_sub = ['eng','math','CHEM']
first_set = {'evening',1}
#adding list to Set using update() method
first_set.update(list_sub)
print('set after adding element :\n',first_set)
Output
set after adding element :
{1, 'eng', 'math', 'CHEM', 'evening'}
2.2 Adding Multiple Lists to Set
update() method can add single or multiply iterable to set. Here we are adding multiple iterables( List)
first_set = {'evening',1}
#list add to set
list_sub = ['eng','math','CHEM']
list_num= [90,100,98]
#adding list
first_set.update(list_sub,list_num)
print('set after adding list elements :\n',first_set)
Output
set after adding list elements :
{1, 98, 'math', 100, 'evening', 'eng', 90, 'CHEM'}
2.3.Add multiple elements of a tuple to the python Set
The Set update() method is used to add multiple elements to the set Here we are adding a single iterable( Tuple).
first_set = {'evening',1}
#tuple add to set
my_tuple = [(1,'eng'),(100,'math'),(98,'CHEM')]
#adding tuple
first_set.update(my_tuple)
print('set after adding tuple elements :\n',first_set)
Output
set after adding tuple elements :
{1, (1, 'eng'), 'evening', (98, 'CHEM'), (100, 'math')}
2.4. Add multiple Tuples to Set using update()
first_set = {'evening',1}
#tuple add to set
my_tuple = [(1,'eng'),(100,'math'),(98,'CHEM')]
fruits_tuple = [(1,'app'),(100,'bana'),(98,'cherry')]
#adding tuples
first_set.update(my_tuple,fruits_tuple)
print('set after adding tuples elements :\n',first_set)
Output
set after adding tuples elements :
{1, (100, 'bana'), (100, 'math'), (98, 'CHEM'), (1, 'eng'), (98, 'cherry'), 'evening', (1, 'app')}
2.5.Python set Add key values using Update()
Important Note: If we adding a dictionary in Set, when we add dictionary as a set element only keys of the dictionary will be added to the set.
first_set = {'evening','good'}
# add dict to set
my_dict = {'eng':90,'math':100,'CHEM':98}
#adding dict
first_set.update(my_dict)
print('set after adding dict elements :\n',first_set)
Output
set after adding dict elements :
{'good', 'evening', 'CHEM', 'eng', 'math'}
2.6.Add multiple Iteratable(list, tuple, dictionary) to Set
Here we are adding multiply iterable( List, Tuple, Dictionary)
Important Note: If we adding a dictionary in Set, when we add dictionary as a set element only keys of the dictionary will be added to the set.
first_set = {'evening','good'}
# add dict to set
my_dict = {'eng':90,'math':100,'CHEM':98}
#tuple add to set
my_tuple = [(1,'eng'),(100,'math')]
#add list to set
list_sub = ['eng','math','CHEM']
#adding dict
first_set.update(my_dict,my_tuple,list_sub)
print('set after adding elements :\n',first_set)
Output
set after adding elements :
{(1, 'eng'), 'math', 'CHEM', 'good', 'eng', 'evening', (100, 'math')}
Conclusion
In this post, we learn how to use update() and add() Set methods in Python set add single and multiples element. By using these methods we can add elements one by one or multiple elements in one shot.