Table of Contents
1. Overview
The Python set update() method updates the set, adding items from other iterables.
The syntax of update() is:
A.update(iterable)
Here, A is a set, and iterable can be any iterable such as list, set, dictionary, string, etc. The elements of the iterable are added to the set A.
Let’s take another example:
A.update(iter1, iter2, iter3)
Here, the elements of iterables iter1, iter2, and iter3 are added to set A.
2. Return Value from update()
This set update() method returns None (returns nothing).
2.1. Example 1: Python set update()
A = {'a', 'b'}
B = {1, 2, 3}
result = A.update(B)
print('A =', A)
print('result =', result)
Output
A = {'a', 1, 2, 3, 'b'}
result = None
2.2. Example 2: Add elements of string and dictionary to Set
string_alphabet = 'abc'
numbers_set = {1, 2}
# add elements of the string to the set
numbers_set.update(string_alphabet)
print('numbers_set =', numbers_set)
info_dictionary = {'key': 1, 'lock' : 2}
numbers_set = {'a', 'b'}
# add keys of dictionary to the set
numbers_set.update(info_dictionary)
print('numbers_set =', numbers_set)
Output
numbers_set = {'c', 1, 2, 'b', 'a'}
numbers_set = {'key', 'b', 'lock', 'a'}
Note: If dictionaries are passed to the
update()method, the keys of the dictionaries are added to the set.
Related posts:
Python Generators
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Find LCM
Python String format()
Python String ljust()
Python String islower()
Python Program to Safely Create a Nested Directory
Python Program to Count the Number of Digits Present In a Number
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Errors and Built-in Exceptions
Python chr()
Python print()
Python String maketrans()
Python Set add()
Python Program to Convert Celsius To Fahrenheit
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Tuple count()
Python iter()
Python Program to Get the File Name From the File Path
Python del Statement
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Reverse a Number
Python String isspace()
Python Program to Find HCF or GCD
Python Program to Differentiate Between type() and isinstance()
Python Program to Check Armstrong Number
Python Program to Trim Whitespace From a String
Python Exception Handling Using try, except and finally statement
Python ascii()
Python Program to Find the Sum of Natural Numbers
Python Set issuperset()
APIs in Node.js vs Python - A Comparison