Python set update() method updates the current set by inserting items of other set elements. If the items of another set are already present in the current, only one item will be present in the resulting set.
Syntax
Parameter Description
Name | Description |
---|---|
set | Specify the main set to which the other set will be added |
set1 | Required. To insert into the main set. |
set2 | optional. Another set in comma separation to add to the set. You can add as many sets as you want to the main set. |
Let’s find out the use of the Python set update()
Function with the examples given below.
Examples of Python Set update() Function
When you update one set to another set in Python, the sequence of resulting items is not in sequence. Every time you refresh the code, you will get a different sequence of sets in each execution.
Example 1
1 2 3 4 5 6 7 8 |
a = {'HTML', 'CSS', 'PHP', 'jQuery', 'javascript'} b = {1, 2, 3, 4} #update b elements to the main set a a.update(b) #print the main set after the update print(a) |
Output
In the above example add a set b to the main set a.
Similarly, you can add another set to the main set of items. Just add another set in comma-separation as given in the example below.
Example 2
1 2 3 4 5 6 7 8 9 |
a = {'HTML', 'CSS', 'PHP', 'jQuery', 'javascript'} b = {1, 2, 3, 4} c = {'x', 'y', 'z'} #update b and c elements to the main set a a.update(b) #print the main set after the update print(a) |
Output