Python Set update() method accepts another iterable such as Set, List, String, Dictionary as a parameter and adds the elements of these iterable to the calling set.
Python Set update()
Python Set update() is a built-in method that is used to add elements from the Set to another Set. The update() function converts the passed iterable to the set before adding their elements to the calling Set.
For example, if A = {1,2,3} and B = {7,8,9} are two sets and we want to update B from A, we just need to write B.update(A) and the new elements in B will be: {1,2,3,7,8,9}.
Syntax
set1.update(set2)
Parameters
The method takes a set as an argument and here set1 will be updated with set2.
But we can update a set with another native data type also (like tuple, list, etc), here will need to write the following code snippet.
set_name.update(set(native_datatype_name))
Return Value
The update() method returns None. It just updates the value of the set.
Update a set with another set
See the following code example.
# app.py # Declaring sets # Set of North India States nstate = {'Kashmir', 'Himachal Pradesh', 'Punjab', 'Uttarakhand'} # Set of South Indian States sstate = {'Kerala', 'Tamil Nadu', 'Andhra Pradesh', 'Telangana'} print("North states are: ", nstate) print("South states are: ", sstate) # declaring an empty set country = set() # Now we will update country with those sets country.update(nstate) # Printing values print("\nStates in country after adding north indian states ", country) country.update(sstate) print("States in country after adding north indian states ", country)
Output
North states are: {'Kashmir', 'Himachal Pradesh', 'Punjab', 'Uttarakhand'} South states are: {'Andhra Pradesh', 'Kerala', 'Tamil Nadu', 'Telangana'} States in country after adding north indian states {'Kashmir', 'Himachal Pradesh', 'Punjab', 'Uttarakhand'} States in country after adding north indian states {'Himachal Pradesh', 'Telangana', 'Andhra Pradesh', 'Punjab', 'Kerala', 'Tamil Nadu', 'Uttarakhand', 'Kashmir'}
In this example, we have first declared two sets containing names of some north Indian states and south Indian states. Then we have declared an empty set named country.
After that, we have first updated the values of the country with a value of north Indian states and then south Indian states.
Update a set with native data type
See the following code.
# Declaring sets # Name of flowers in a list flower = ['rose', 'tulip', 'poppy'] # Name of fruits in a tuple fruits = ('apple', 'banana', 'orange') print("Flowers are: ", flower) print("Fruits are: ", fruits) # declaring new empty set mix = set() # Now we will update these values in set mix mix.update(list(flower)) print("After adding flowers elements in mix are: ", mix) mix.update(tuple(fruits)) print("After adding fruits elements in mix are: ", mix)
Output
Flowers are: ['rose', 'tulip', 'poppy'] Fruits are: ('apple', 'banana', 'orange') After adding flowers elements in mix are: {'poppy', 'tulip', 'rose'} After adding fruits elements in mix are: {'orange', 'rose', 'poppy', 'banana', 'tulip', 'apple'}
In this example, we have first declared one list of some flowers name, then a tuple of some fruit name. After that, a new empty set named mix is declared. Then we have updated the value of mix with native data type flowers and fruits respectively.
That’s it for this tutorial.
See also
Python set symmetric_difference()
Python set intersection_update()