How to Convert List to Set in Python
![how to convert list to set in python](https://pythonpoint.net/wp-content/uploads/2020/12/how-to-convert-list-to-set-in-python-1024x576.jpg)
In this article, we will see how to convert list to set.
We can use the Python set() function to convert list to set.
In Python Sets, duplicates are not allowed, hence when a list is converted to set, all duplicates will be removed in the resultant set.
list1 = [1, 2, 5, 2, 4, 3, 1, 5] print("Original list: ", list1) set1 = set(list1) print("New set: ", set1)
Output:
Original list: [1, 2, 5, 2, 4, 3, 1, 5] New set: {1, 2, 3, 4, 5}
As you can see duplicates are removed when converted to list and square brackets are replaced with curly brackets.
Subscribe
Login
Please login to comment
0 Discussion