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 Check Whether a String is Palindrome or Not
Python Global, Local and Nonlocal variables
Python compile()
Python List Comprehension
Python isinstance()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Convert Celsius To Fahrenheit
Python String isdecimal()
Python slice()
Python Program to Delete an Element From a Dictionary
Python String isidentifier()
Python Program to Parse a String to a Float or Int
Python Program to Merge Mails
Python String format_map()
Python Data Structures and Algorithms - Benjamin Baka
Python Set issuperset()
Python Recursion
Python __import__()
Python Program to Slice Lists
Python Program to Add Two Numbers
Python callable()
Python Dictionary popitem()
Python dict()
Python Program to Find HCF or GCD
Python open()
Python Data Types
Python List count()
Python Program to Make a Simple Calculator
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python String rindex()
Python Set copy()
Python Generators