Python update() function in set adds elements from a set (passed as an argument) to the set. If the element is present in both of the sets then the element comes only one time in the updated set.
Python Set update() Method Syntax
Syntax : set1.update(set2)
Here set1 is the set in which set2 will be added.
Parameters : Update() method takes any number of argument. The arguments can be a set, list, tuples or a dictionary. It automatically converts into a set and adds to the set.
Return value : This method adds set2 to set1 and returns nothing.
Set update() Method Example
Working with Python set update List
In this example there are three list initially. The first and second list is converted to the set using the set() function. The update()
method is then used to merge the elements of ‘set2'
into ‘set1'
, resulting in a combined set. The code prints this updated set. Next, the update()
method is applied again to include the elements from a third list, ‘list3'
, into the already updated ‘set1'
. Finally, the code prints the final merged set, which contains elements from ‘set2'
, ‘list3'
, and the original ‘set1'
.
Python3
list1 = [ 1 , 2 , 3 ]
list2 = [ 5 , 6 , 7 ]
list3 = [ 10 , 11 , 12 ]
set1 = set (list2)
set2 = set (list1)
set1.update(set2)
print (set1)
set1.update(list3)
print (set1)
|
Output :
{1, 2, 3, 5, 6, 7}
{1, 2, 3, 5, 6, 7, 10, 11, 12}
Python Set update Element in Set
This Python program explains the update()
method for sets. Initially, two lists, ‘list1'
and ‘list2'
, containing integers, are converted into sets, ‘set1'
and ‘set2'
. The update()
method is then used to merge the unique elements of ‘set2'
into ‘set1'
, creating a consolidated set. This set is printed to display the combined elements. Furthermore, the update()
method is applied to include the elements from the ‘alphabet_set'
, expanding the set to encompass characters ‘a’, ‘b’, and ‘c’. The final updated ‘set1'
is printed again, now elements from both ‘set2'
and ‘alphabet_set'
.
Python3
list1 = [ 1 , 2 , 3 , 4 ]
list2 = [ 1 , 4 , 2 , 3 , 5 ]
alphabet_set = { 'a' , 'b' , 'c' }
set1 = set (list2)
set2 = set (list1)
set1.update(set2)
print (set1)
set1.update(alphabet_set)
print (set1)
|
Output :
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 'c', 'b', 'a'}
Add Elements of the Dictionary to Set
This Python code updates a set named number
with elements from a dictionary called ‘num_Dict'
. Initially, number
contains integers from 1 to 5, while ‘num_Dict'
stores integer-to-string mappings for numbers 6 to 10. The update()
method merges the keys from ‘num_Dict'
into the ‘number'
set, resulting in a set containing unique elements from both the original set and the dictionary. The output displays the updated set, which now includes integers from 1 to 10, combining numeric and textual representations.
Python3
number = { 1 , 2 , 3 , 4 , 5 }
num_Dict = { 6 : 'Six' , 7 : 'Seven' , 8 : 'Eight' ,
9 : 'Nine' , 10 : 'Ten' }
number.update(num_Dict)
print ( "Updated set: " , number)
|
Output:
Updated set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!