Python Convert list to set

In this tutorial, we will see how to convert list to set.
You can use python set() function to convert list to set.It is simplest way to convert list to set.

As Set does not allow duplicates, when you convert list to set, all duplicates will be removed in the set.

Let’s understand with the help of example.

listOfStudents=['Mohan','John','Ramesh','Mohan','John']
print("List of Students:",listOfStudents)
uniqueNames=set(listOfStudents)
print("Set of uniqueNames:",uniqueNames)

listOfMarks=[8,7,6,7,8,8,7,6,7,8,9]
print("List of Makrs:",listOfMarks)
setOfMarks=set(listOfMarks)
print("Set of marks:",setOfMarks)

Output:

List of Students: [‘Mohan’, ‘John’, ‘Ramesh’, ‘Mohan’, ‘John’]
Set of uniqueNames: {‘John’, ‘Ramesh’, ‘Mohan’}
List of Makrs: [8, 7, 6, 7, 8, 8, 7, 6, 7, 8, 9]
Set of marks: {8, 9, 6, 7}

As you can see, set starts with open curly braces and ends with close with braces.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.