In this Python tutorial, we’ll learn how to properly use Python set update method. First, we’ll see its role and after that, we’ll use it in practical code examples in order to better understand its proper usage.
Introduction: Python Set Update Method
The update() method is a method of Python set. It’s used to update its calling set with the items of iterables passed to it as an argument. These iterables can be Python lists, sets, tuples, dictionaries or Python strings.
First we’ll try to understand the syntax of update method. After that, we’ll use it in Python programs to better understand its implementation.
Syntax of Update Method
set.update(one or more iterables)
- This method can take one or more arguments. They can be a Python tuples, lists, sets, strings or dictionaries.
- It does not return anything. It just updates its calling set with the elements of all iterables passed to it as arguments.
- Passing no argument to update method will result in no changes made to its calling set.
Implementing Python Set Update Method (Multiple examples)
In order for detailed understanding of this method, we’ve to follow the below examples.
Example 1: update() Method with Sets
aa = {'B','C','D','E','F','G'} bb = {'D','F','C','H','K'} aa.update(bb) print('First set: ',aa) print('Second set: ',bb)
Output
First set: {'E', 'G', 'F', 'H', 'K', 'B', 'D', 'C'} Second set: {'F', 'H', 'K', 'D', 'C'}
- We’ve created 2 Python sets.
- Then we’ve used the update method of first set(aa) and have passed second set(bb) as an argument to it.
- Finally, we’ve displayed both these set and we can see that the calling set(aa) has been updated. It shows items of second set(bb) as well. While second set(bb) remains the same.
Example 2: Multiple Arguments
a = {1,4,5} b = {4,7,8,9} c = {'a','b',43} a.update(b,c) print(a)
Output
{1, 4, 5, 7, 8, 9, 43, 'b', 'a'}
We can also pass multiple arguments to Python set update method which has been demonstrated in this program.
Example 3: Passing other Iterables as Arguments
x = {2,3,4,5} x.update([1,2]) x.update((8,4)) x.update({'id':2,'number':345}) x.update('Python') x.update([2,17],(8,'x')) print(x)
Output
{1, 2, 3, 4, 5, 'id', 'h', 8, 'n', 'x', 'P', 'number', 17, 'o', 't', 'y'}
- In this program, first we’ve specified a simple Python set.
- Then we’ve passed iterables to its update method. These are list, tuple, dictionary and string. We also have passed multiple iterable arguments to it.
- Finally, we’ve printed the calling set(x) and it shows an updated version.
This method will only take the keys of dictionary as items and updates the calling set with them. In case of string, it’ll break the string in single characters and add it to the calling set.
Example 4: Passing No Arguments
y={'a','b'} y.update() print(y)
Output
{'b', 'a'}
We can see that passing no arguments to the update method will result in no changes made to its calling set.
If the calling set is empty and no arguments has been passed to the update method then still no changes will be made to the calling set. See below code:
y=set() # set() denotes an empty set y.update() print(y)
Output
set()
Example 5: Return Value Of Python Set Update Method
i={11,44,55} j={44,88,96} returnedVal=i.update(j) print(returnedVal)
Output
None
- In this code, we’ve first created 2 Python sets.
- Then we’ve applied update method on them and have stored its return value inside a variable.
- Finally, we’ve displayed value of this variable and it shows None. It means this method does not return anything. It just updates its calling set with the items of iterables passed to it as arguments. See below:
print(i) print(j)
Output
{96, 55, 88, 11, 44} {88, 44, 96}
We can see that the calling set(set(i) that calls the update method) has been updated while the second set(j) remains the same.
Example 6: update() Method applied on Python Lists of Sets Using Python For Loop
lstX = [{'x','y','z'},{1.1,2.2,3.3},{8,7,6}] lstY = [{'w','p','z'},{21.1,2.2,3.3},{8,6,5,63}] newLst = [] for var1,var2 in zip(lstX,lstY): var1.update(var2) newLst.append(var1) print(newLst)
Output
[{'p', 'y', 'z', 'x', 'w'}, {1.1, 2.2, 3.3, 21.1}, {5, 6, 7, 8, 63}]
- In this program, we’ve first created 2 Python list of sets. Also, we’ve created an empty list to store the updated values.
- Then we’ve used Python for loop which will take elements(sets) of both Python lists while iterating over them. Click here to learn more about iterating over multiple Python lists simultaneously.
- Inside the body of this loop, we’ve first applied update method which will get called using the items(sets) of first list(lstX) and will take items(sets) of second list(lstX) as its arguments.
- We’ve added the updated sets(lstX) to new list using its append method.
- Finally, we’ve displayed the new list and it shows the expected result.
- Actually, we don’t have to specify a new list. Instead, we can just display the list whose items(sets) were used to call the update method(lstX in this case). See below code:
print(lstX)
Output
[{'w', 'x', 'p', 'y', 'z'}, {1.1, 2.2, 3.3, 21.1}, {5, 6, 7, 8, 63}]
We can see that it shows the same updated result. Reason is that the update method updates its calling set and items(sets) of this list(lstX) were used to call this method so they get updated.
This is the proper use of Python set update method. Hope you like this post and if you do, then do share it with other Python developers.
Click here to learn more about Python set methods.
Do feel free to ask in the comment section if you still have any questions related to Python set update method. We’ll be very delighted to answer all of your questions.
Conclusion
In conclusion, hope you now have a detailed practical knowledge of how to properly use Python set update method. Also, do feel free to share your amazing feedback with us by using the comment section.
Other worth reading posts on Python programming are also available on this site. Feel free to pay a visit to them as well. Thank you for reading this article.