Python

Python set update() method

Python has many iterable objects such as sets, lists, dictionaries, etc. The set is a very useful data type of Python that is a collection of unordered elements. The Python set update() method updates any set by adding new elements from another set or iterable object. The duplicate elements of both sets will be omitted when updating. The elements of one or more sets can be added to the main set. The uses of the update() method have been shown in this tutorial.

Syntax:

The following syntax shows that the values of an iterable object will be added to an existing set.

set.update(iterable)

or

The following syntax shows that the values of two or more iterable objects will be added to an existing set.

set.update(iterable1, iterable2, iterable3,…)

The iterable object converts automatically into a set before adding values to another set, and the update() function returns nothing. Different uses of the update() function have been shown in the next part of the tutorial.

Example-1: Update a set by another set

Create a python file with the following script that will insert the values of a set to another set. Here, the values of both sets are numeric. The values of the two sets and the values of the updated set will be printed later.

#Declare the first set
set1 = {45, 10, 38, 21, 90, 42, 37}
#Declare the second set
set2 = {21, 49, 60, 90, 11, 56}

#Print the values of the first set
print("The values of the first set:\n",set1)
#Print the values of the second set
print("The values of the second set:\n",set2)

#Update the first set by the second set
set1.update(set2)
#Print the values of the updated set
print("The values of the first set after update:\n",set1)

Output:
The following output will appear after executing the above script.

Example-2: Update a set of numbers by a set of characters

Create a python file with the following script that will insert the values of a set to another set. Here, the values of the main set are numbers, and the values of another set are strings. The values of the updated set will be printed later.

#Declare a list of numbers
listdata = [90, 50, 10, 60, 40, 30]
#Convert the list into a set
set1 = set(listdata)

#Print the values of the set
print("The values of the original set:\n",set1)

#Declare another set
set2 = {'PHP', 'BASH', 'JAVA'}

#Update the set1 by set2
set1.update(set2)

#Print the values of the updated set
print("The values of the updated set:\n",set1)

Output:
The following output will appear after executing the above script.

Example-3: Update a set by using the list

Create a python file with the following script that will insert the values of two lists into another list that will be converted into a set before update. Here, the values of three lists are characters. The values of the main set will be printed before and after updating the set.

#Declare three lists of characters
listdata1 = ['a', 'b', 'c', 'd']
listdata2 = ['p', 'q', 'r']
listdata3 = ['w', 'x', 'y', 'z']

#Create set from the first list
setdata = set(listdata1)

#Print the values of the set
print("The values of the original set:\n",setdata)

#Update set by the second list
setdata.update(listdata2)
#Print the values of the set
print("The values of the set after updating by the second list:\n",setdata)

#Update set by the third list
setdata.update(listdata3)
#Print the values of the set
print("The values of the set after updating by the third list:\n",setdata)

Output:
The following output will appear after executing the above script.

Example-4: Update a set by using a dictionary

Create a python file with the following script that will insert the values of a dictionary into a set. Here, the values of the main set are characters, and the dictionary’s keys are characters. The values of the main set will be printed before and after updating the set.

#Declare a set
setdata = {'a', 'b', 'c', 'd', 'e'}
#Declare a dictionary
dicdata = {'x':60, 'y':50, 'z':36}

#Print the values of the set
print("The values of the set:\n",setdata)
#Print the values of the dictionary
print("The values of the dictionary:\n",dicdata)

#Update set by the dictionary
setdata.update(dicdata)

#Print the values of the set
print("The values of the set after updating by the dictionary:\n",setdata)

Output:
The following output will appear after executing the above script.

Example-5: Update a set by using a tuple

Create a python file with the following script that will insert the values of a tuple into a set. Here, the values of the main set and the tuple’s values are numbers. The values of the main set will be printed before and after updating the set.

#Declare a set
setdata = {7, 8, 4, 9, 2, 0, 9}
#Declare a tuple
tupledata = (70, 30, 60, 40, 10)

#Print the values of the set
print("The values of the set:\n",setdata)
#Print the values of the tuple
print("The values of the tuple:\n",tupledata)

#Update set by the tuple
setdata.update(tupledata)

#Print the values of the set
print("The values of the set after updating by the tuple:\n",setdata)

Output:
The following output will appear after executing the above script.

Element-6: Update a set by using a string

Create a python file with the following script to insert a string value into a set. Here, the values of the main set are strings. The values of the main set will be printed before and after updating the set.

#Declare a set
setdata = {'Ubuntu','windows','Fedora','Red Hat'}
#Declare a string
strval = 'Linux'

#Print the values of the set
print("The values of the set:\n",setdata)
#Print the values of the string
print("The values of the string:\n",strval)

#Update set by the string
setdata.update(strval)

#Print the values of the set
print("The values of the set after updating by the string:\n",setdata)

Output:
The following output will appear after executing the above script.

Conclusion:

Different ways to update a set by using another set, list, tuple, dictionary, and string have been described in this tutorial to help the python users know the purpose of using the update() function for updating the set.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.