English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Méthodes de l'ensemble en Python
The Python set update() method updates the set and adds items from other iterable objects.
The syntax of update() is:
A.update(iterable)
Here, A is a set, and iterable can be any iterable object, such as a list, set, dictionary, string, etc. Add the elements of the iterable to the set A.
Let's take another example:
A.update(iter1, iter2, iter3)
Here, the elements of iterables are iter1, iter2and iter3is added to the set A.
The update() method returns None (returns nothing).
A = {'a', 'b'} B = {1, 2, 3} result = A.update(B) print('A =', A) print('result =', result)
Output result
A = {'a', 1, 2, 3, 'b'} result = None
string_alphabet = 'abc' numbers_set = {1, 2} # Add the 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 the keys of the dictionary to the set numbers_set.update(info_dictionary) print('numbers_set =', numbers_set)
Output result
numbers_set = {'c', 1, 2, 'b', 'a'} numbers_set = {'key', 'b', 'lock', 'a'}
Note:If a dictionary is passed to the update() method, the keys of the dictionary are added to the set.