Python Set update() method
Python Set update() method
The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item will be updated in the original set.
Syntax
set.update(set1)
Parameter
set- This parameter represents the main set.
set1- This argument signifies the other set to search for equal items.
Return
None
Example 1
# Python program explaining # the set.update() method #initializing set1 set1 = {"mango", "banana", "apple","tomato"} print("Set 1:",set1) # initializing set2 set2 = {"potato", "spinach","Carrot", "apple"} print("Set 2:",set2) # updates the current set1, by adding items from set2 set1.update(set2) print("After the update() method...") print("Updated Set 1:",set1) print("Set 2:",set2)
Output
Set 1: {'tomato', 'apple', 'banana', 'mango'} Set 2: {'apple', 'Carrot', 'spinach', 'potato'} After the update() method... Updated Set 1: {'apple', 'tomato', 'potato', 'Carrot', 'mango', 'spinach', 'banana'} Set 2: {'apple', 'Carrot', 'spinach', 'potato'}
Example 2
# Python program explaining # the set.update() method # initializing set A A = {"a","b","c","v"} print("Set A:",A) #initializing B B = {"a","b","c","h","k","r"} print("Set B:",B) #the common items will have only one appearance in Set A A.update(B) print("\nUpdated A:",A) print("Set B:",B)
Output
Set A: {'b', 'a', 'v', 'c'} Set B: {'k', 'h', 'r', 'a', 'b', 'c'} Updated A: {'k', 'h', 'v', 'r', 'a', 'b', 'c'} Set B: {'k', 'h', 'r', 'a', 'b', 'c'}