AppDividend
Latest Code Tutorials

How to Convert Python List to Set

0

To convert set to list in Python, use the list() method. Lists are used to save multiple elements in a single variable. To create a list, use the square brackets([ ]) in Python.

Convert Python List to Set

To convert list to set in Python, use the set() function. The set() function in Python is an inbuilt function that creates a set. Sets are used to store multiple items in a single variable.

The set() function takes an optional parameter that can be a sequence like string, tupleor collection(set, dictionary), or an iterator object converted into a 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.

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 from the data type from the list to set.

If you want to convert the list to set using a for loop iteration then use the following code.

Converting list containing duplicate items to set

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 set, duplicate items are removed, and in the Set, it only appears one time.

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}

That is it for converting list data type to set in Python.

See also

Python list to csv

Python list to array

Python list to tuple

Python list to string

Python list to dataframe

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.