Python Convert list to set

Previous
Next

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.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog