close

How to add a list to a set in python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about how to add a list to a set in python. so without wasting time lets learn about of this.

How to add a list to a set in python

  1. add a list to a set in python

    to add a list to a set in python just Use set.add().By using set.add() you can add a list to a set in python. Lets learn about of this by given below example:
    myset = set((11,21,31,41)) mylist = list([11,21,31]) myset.add(tuple(mylist)) print(myset) Output : {(11, 21, 31), 41, 11, 21, 31}

  2. How to add a list to a set in python

    to add a list to a set in python just Use set.update().By using set.update() you can add a list to a set in python. Lets learn about of this by given below example: myset = set((11,21,31,41)) mylist = list([22,54,56]) myset.update(tuple(mylist)) print(myset) Output : {41, 11, 21, 22, 54, 56, 31}

  3. python set add

    To add a list to a set in python just Use set.update().By using set.update() you can add a list to a set in python. Lets learn about of this by given below example: myset = set((11,21,31,41)) mylist = list([22,54,56]) myset.update(tuple(mylist)) print(myset) Output : {41, 11, 21, 22, 54, 56, 31}

Method 1: Use set.add() To add List to set

By using set.add() you can add a list. Lets learn about of this by given below example:

myset = set((11,21,31,41))
mylist = list([11,21,31])
myset.add(tuple(mylist))
print(myset)

Output :

{(11, 21, 31), 41, 11, 21, 31}

Method 2: Use set.update()

By using set.update() you can add a list to a set. Lets learn about of this by given below example:

myset = set((11,21,31,41))
mylist = list([22,54,56])
myset.update(tuple(mylist))
print(myset)

Output :

{41, 11, 21, 22, 54, 56, 31}

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment