Q:

Given a Python list, Write a program to add all its elements into a given set.

belongs to collection: Python Set Exercises

0

Add a list of elements to a set

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'}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use the update() method of a set.

Solution:

sample_set = {"Yellow", "Orange", "Black"}
sample_list = ["Blue", "Green", "Red"]

sample_set.update(sample_list)
print(sample_set)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Return a new set of identical items from two sets ... >>