how to convert a list in a set in python

To convert a list into a set in Python, you can simply use the set() function. Here's the syntax:

main.py
my_list = [1, 2, 3, 4, 5]

my_set = set(my_list)

print(my_set)
64 chars
6 lines

Output:

main.py
{1, 2, 3, 4, 5}
16 chars
2 lines

In this example, we first create a list called my_list. Then we use the set() function with our list as an argument to create a set called my_set. Finally, we print my_set which now contains all elements from my_list as a set.

gistlibby LogSnag