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 Dictionary values()
Python String casefold()
Python Exception Handling Using try, except and finally statement
Python tuple()
Python Program to Remove Punctuations From a String
Python List clear()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python String isdigit()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Safely Create a Nested Directory
Python type()
Python ascii()
Python Program to Copy a File
Python Program to Make a Flattened List from Nested List
Python String maketrans()
Python vars()
Python String translate()
Python Program to Check Whether a String is Palindrome or Not
Python Matrices and NumPy Arrays
Python Deep Learning Cookbook - Indra den Bakker
Python ord()
Python String isupper()
Python Program to Sort Words in Alphabetic Order
Python String lstrip()
Python Dictionary copy()
Python Errors and Built-in Exceptions
Python Program to Count the Occurrence of an Item in a List
Python hasattr()
Python List copy()
Python Program to Get a Substring of a String
Python String find()
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set