List to set Python | Conversion example code

Use set() function to convert list to set in Python. Sets are not allowed duplicate elements (values), if you convert list to 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.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

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