How to Add a List to a Set in Python


If you need to add a list to a set in Python, then you can do the following:

Option 1 – Using Tuple

myset = set((1,2,3,4))
mylist = list(1,2,3])

myset.add(tuple(mylist))
print(myset)

Output: {1, 2, 3, 4, (1, 2, 3)}

Option 2 – Using set.update()

myset = set((1,2,3,4))
mylist = list(8,9,12])

myset.update(tuple(mylist))
print(myset)

Output: {1, 2, 3, 4, 8, 9, 12}