belongs to collection: Python Set Exercises
Given a Python list, Write a program to add all its elements into a given set.
Given:
sample_set = {"Yellow", "Orange", "Black"} sample_list = ["Blue", "Green", "Red"]
Expected output:
Note: Set is unordered.
{'Green', 'Yellow', 'Black', 'Orange', 'Red', 'Blue'}
Hint:
Use the update() method of a set.
update()
Solution:
sample_set = {"Yellow", "Orange", "Black"} sample_list = ["Blue", "Green", "Red"] sample_set.update(sample_list) print(sample_set)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Hint:
Use the
update()
method of a set.Solution:
need an explanation for this answer? contact us directly to get an explanation for this answer