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:
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python List clear()
Python List copy()
Python Program to Add Two Matrices
Introduction to Scientific Programming with Python - Joakim Sundnes
Python *args and **kwargs
Python sorted()
How to Get Started With Python?
Python Program to Add Two Numbers
Python Decorators
Python Program to Get the File Name From the File Path
Python Program to Create a Long Multiline String
Python Program to Find Sum of Natural Numbers Using Recursion
Python format()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Set issuperset()
Python issubclass()
Python Program to Measure the Elapsed Time in Python
Python String maketrans()
Python String isalpha()
Python Program to Find the Factors of a Number
Python vars()
Deep Learning in Python - LazyProgrammer
Python range()
Python Program to Extract Extension From the File Name
Python Iterators
Python String encode()
Python String casefold()
Python Set copy()
Python Strings
Python while Loop
Python getattr()