How to Convert List to Set in Python

To convert a list to a set in Python, you can use the set() function.

The following example shows how to convert a list to a set in Python using the set() function.

Using set() Function

We can use the set() function to convert a list to a set.

Suppose we have the following list:

# Declare list
my_list = [1, 2, 2, 3, 3, 3]

# Convert list to set
my_set = set(my_list)

# Show result
print(my_set)

Output: 👇️

{1, 2, 3}

In this example, we use the set() function to convert the list my_list to a set my_set. The output shows the set created from the list using the set() function.

Conclusion

We can use the set() function to convert a list to a set in Python. This method provides a convenient way to remove duplicates and transform lists into sets.