How to convert list to set in Python

In this post, we will see how to convert list to set in Python.
It is very easy to convert list to set in Python.Please note that list can have duplicates but set can not have duplicates.
So when you convert from list to set, duplicate will be removed in this process.
You can simply use set function to convert list to set in Python.
Let’s understand with the help of example.

l=[1,3,3,4,5,6,4,3,4,5,6,4,2] s=set(l) print(s) lCountry=["india","china","bhutan","india","china"] sCountry=set(lCountry) print(sCountry)

Output:

{1, 2, 3, 4, 5, 6}
{‘china’, ‘india’, ‘bhutan’}

As you can see, duplicates got removed when we converted from list to set in Python.
That’s all about python list to set conversion.

Leave a Reply

Your email address will not be published. Required fields are marked *