Python Set | update()

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}


Contact Us