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 Program to Print Hello world!
Python memoryview()
Python Program to Randomly Select an Element From the List
Python String startswith()
Python Program to Check Armstrong Number
Python Dictionary update()
Python round()
Python Program to Iterate Through Two Lists in Parallel
Python Program to Iterate Over Dictionaries Using for Loop
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Print Colored Text to the Terminal
Python pow()
Python Program to Remove Punctuations From a String
Python Program to Differentiate Between type() and isinstance()
Python enumerate()
Python String splitlines()
Python Dictionary values()
Python List index()
Python Program to Solve Quadratic Equation
Python Program to Add Two Numbers
Python Variables, Constants and Literals
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Convert String to Datetime
Python bytes()
Python Program to Extract Extension From the File Name
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Display the multiplication Table
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python String rpartition()
Python tuple()