Add A List Of Elements To A Set In Python
Chapter:
Python
Last Updated:
19-05-2023 03:11:19 UTC
Program:
/* ............... START ............... */
my_set = {1, 2, 3}
my_list = [4, 5, 6]
my_set.update(my_list)
print(my_set)
/* ............... END ............... */
Output
Notes:
-
In the above example, the update() method is used to add the elements from my_list to the my_set set. The resulting set contains all the elements from both the original set and the list.
- Note that the update() method modifies the set in place and does not return a new set.