Adding Multiple Elements to a Set in Python
The add method can be used to add elements to a set one by one. But when you need to add several elements at once, the update method is used.
Let us have a set:
st = {'a', 'b', 'c', 'd'}
If you pass a string to the method, it will be added to the set as a list of elements. Let's add the string 'xyz' to the set:
st.update('xyz')
print(st) # {'z', 'y', 'd', 'c', 'x', 'a', 'b'}
In the parameter of the update method, you can specify lists to add to the set:
st.update(['1', '2', '3'])
print(st) # {'1', 'b', 'a', 'd', 'c', '3', '2'}
Elements of tuples can also be added to a set:
st.update((1, 2, 3))
print(st) # {'a', 'c', 1, 2, 3, 'd', 'b'}
But when passing dictionaries to a set, only its keys will be added:
st.update({1: 'text1', 2: 'text2'})
print(st) # {1, 'd', 2, 'a', 'b', 'c'}
Given a set:
st = {'x', 'y', 'z', 'w'}
Add to it the line 'abxcz'.
Given a set:
st = {1, 2, 3}
The list is also given:
lst = [3, 4, 5, 6]
Add the list items to our set.
The following code is given:
st = {'12', '34', '56'}
tlp = (2, 4, 6)
st.update(tlp)
print(st)
Tell me what will be output to the console.