close

How to add a list to a set in python

Hi guys. Ho are you all? I hope you all fine. Today in this tutorial we learn about How to add a list to a set in python. Here i am giving you some methods by using them you can add a list to a set in python. This is very easy and also very useful in python. set are the immutable collection of the objects. and the list are mutable collection of the objects or elements. Here we will learn to add a list to set in python. So lets learn about of this by given below methods. I hope this will help you. Les learn 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 tuple. By using tuple we can add a list to a set in python. So in this method we will learn about of that. Here first of all we will make a set and after it a list which we want to add to a set. And then use a set.add() function and use tuple and then print a set and it will add alist to a set as you can see in my output. So lets learn this method by given below example. I hope this will help you. lets learn : myset = set((5,6,8,1)) mylist = list([10,15,54]) myset.add(tuple(mylist)) print(myset) Output : {1, 5, 6, 8, (10, 15, 54)}

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

    to add a list to a set in python just Use set.update(). You can use set.update() function to add a list to a set in python. here we will learn about of that. First of all make a set and then make a list you want to add in the set and then use this set.update() function and print your list. It will simply add the list to your set. Lets learn this: myset = set((5,6,8,1)) mylist = list([10,15,54]) myset.update(tuple(mylist)) print(myset) Output : {1, 5, 6, 8, 10, 15, 54}

Method 1: Use tuple

By using tuple we can add a list to a set in python. So in this method we will learn about of that. Here first of all we will make a set and after it a list which we want to add to a set. And then use a set.add() function and use tuple and then print a set and it will add alist to a set as you can see in my output. So lets learn this method by given below example. I hope this will help you. lets learn :

myset = set((5,6,8,1))
mylist = list([10,15,54])
myset.add(tuple(mylist))
print(myset)

Output :

{1, 5, 6, 8, (10, 15, 54)}

Method 2: Use set.update()

You can use set.update() function to add a list to a set in python. here we will learn about of that. First of all make a set and then make a list you want to add in the set and then use this set.update() function and print your list. It will simply add the list to your set. Lets learn this:

myset = set((5,6,8,1))
mylist = list([10,15,54])
myset.update(tuple(mylist))
print(myset)

Output :

{1, 5, 6, 8, 10, 15, 54}

Conclusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment