set.update(*iterables) adds the elements produced by one or more iterables to an existing set and returns None. Duplicate values are stored only once.
Because a set is mutable, update() changes it in place. Use add() when you want to add one hashable object as a single element.
my_set={'a','b','c','x'}
my_set2={'x','y','z'}
my_set.update(my_set2)
print(my_set)
Output is here
{'z', 'a', 'x', 'y', 'b', 'c'}
In above code x is added once only as no duplicate value is kept.
my_set={'a','b','c','x'}
text='alex'
my_set.update(text)
print(my_set)
Output
{'a', 'b', 'c', 'x', 'l', 'e'}
my_set={'a','b','c','x'}
my_list=['x','y','z']
my_set.update(my_list)
print(my_set)
Output
{'z', 'a', 'y', 'b', 'x', 'c'}
my_set={'a','b','c','x'}
my_tuple=('x','y','z')
my_set.update(my_tuple)
print(my_set)
Output
{'z', 'a', 'y', 'b', 'x', 'c'}
Values already present are not duplicated; a set stores each distinct value only once.
add() to add one hashable object as a single element. Use update() to consume the elements of one or more iterables such as a list, tuple or string.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.