Skip to content

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)
Code language: Python (python)

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)
Code language: Python (python)

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

See also  Approaches to software reuse
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x