The main difference between a list vs. set in Python is that the set cannot contain duplicate elements while a list can contain duplicate elements.
Python list
Lists in Python are used to save multiple elements in a single variable. Lists are just like dynamic-sized arrays.
Python set
Set data type in Python does not allow duplicate elements; when you convert the list to set, all duplicate elements will be removed from the set.
Python list to set
To convert a list to set in Python, use the set() function. The set() is a built-in Python function that creates a set from an object. Sets are used to store multiple elements in a single variable.
The set() function takes an optional parameter, a sequence like string, tuple, or collection(set, dictionary), or an iterator object converted into a set.
s_education = ["Otis", "Maeve", "Eric", "Jackson", "Ola"] print("The characters in List are: ", s_education) set_items = set(s_education) print("The characters in Set are: ", set_items)
Output
The characters in List are: ['Otis', 'Maeve', 'Eric', 'Jackson', 'Ola'] The characters in Set are: {'Maeve', 'Ola', 'Eric', 'Otis', 'Jackson'}
You can see that the set() function has converted the data type from the list to the set.
Converting list containing duplicate items to set
The set cannot contain duplicate elements, and if we try to convert a list filled with duplicate elements to the set, it will remove the duplicate elements.
Let’s define a list that contains duplicate items.
s_education = ["Otis", "Maeve", "Eric", "Maeve", "Ola", "Otis"] print("The characters in List are: ", s_education) set_items = set(s_education) print("The characters in Set are: ", set_items)
Output
The characters in List are: ['Otis', 'Maeve', 'Eric', 'Maeve', 'Ola', 'Otis'] The characters in Set are: {'Maeve', 'Eric', 'Otis', 'Ola'}
You can see that Otis and Maeve appeared two times on the list, and after converting a list to a set, duplicate items are removed, and in the Set, it only appears once.
To check the data type in Python, use the type() function.
s_education = ["Otis", "Maeve", "Eric", "Maeve", "Ola", "Otis"] print(s_education) set_items = set(s_education) print(set_items) print(type(set_items))
Output
['Otis', 'Maeve', 'Eric', 'Maeve', 'Ola', 'Otis'] {'Eric', 'Ola', 'Otis', 'Maeve'} <class 'set'>
You can see that the data type of output is set.
The set() Constructor
The set() constructor is used to make a set. If no parameters are passed, then an empty set is returned.
set_items = set() print(set_items)
Output
set()
You can create sets from string, tuple, list, and range.
Let’s see the following example.
# from string print(set('Wonder Woman')) # from tuple print(set(('l', 'o', 'k', 'i'))) # from list print(set([19, 21, 11, 29, 46])) # from range print(set(range(5)))
Output
{'n', 'm', 'a', 'd', 'o', ' ', 'e', 'r', 'W'} {'k', 'i', 'o', 'l'} {11, 46, 19, 21, 29} {0, 1, 2, 3, 4}
Python set to list
To convert a set to list in Python, use the list() method. To create a list, use the square brackets([ ]).
That is it for converting the list to set in Python.