Skip to content

List to set Python | Conversion example code

  • by

Use the set() function to convert a list to a set in Python. Sets are not allowed duplicate elements (values), if you convert a list to a set then all duplicates will be removed in the new set.

set(iterable)

Example List to set Python

Simple example code using a list of numbers to convert into a set.

my_list = [8, 7, 6, 7, 8, 8, 7, 6, 7, 8, 9]

my_set = set(my_list)

print(my_set)

Output: See output set starts with open curly braces and closes with curly braces.

List to set Python

Another example using the list to strings

my_list = ['A', 'B', 'C', 'B']

my_set = set(my_list)

print(my_set)

Output: {‘A’, ‘C’, ‘B’}

Do comment if you have any doubts and suggestions on this Python list set topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading