Convert Set to List in Python (5 Examples)
Welcome! This tutorial will show you 5 simple ways of converting a set to a list in Python programming.
First, though, here is a quick glance at the tutorial:
Let’s get into it!
Create Sample Set
Run the code below in your Python IDE to create a sample set.
my_set = {1, 2, 3}
Example 1: list() Function
In this example, we will use the Python list()
function to convert the set into a list.
my_set = {1, 2, 3} my_list = list(my_set) print(my_list) # [1, 2, 3]
Example 2: sorted() Function
Here, we will use the Python sorted()
function to convert the set into a list.
my_list = sorted(my_set) print(my_list) # [1, 2, 3]
Example 3: `*` Operator
In this next example, we will use the *
operator to convert the set into a list.
my_list = [*my_set] print(my_list) # [1, 2, 3]
Example 4: List Comprehension
Here, we will use list comprehension to convert the set into a list.
my_list = [x for x in my_set] print(my_list) # [1, 2, 3]
Example 5: For Loop
In this last example, we will use a for loop to convert the set into a Python list.
my_list = [] for i in my_set: my_list.append(i) print(my_list) # [1, 2, 3]
That’s it! I hope you found this tutorial helpful.
Video, Further Resources & Summary
Do you need more explanations on how to convert set to list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert sets to lists in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to checkout other Python list tutorials on Statistics Globe, starting with these ones:
- Check if String Exists in List in Python (3 Examples)
- Convert List from Character String to Integer in Python (2 Examples)
- Access List Element by Index in Python (3 Examples)
- Lists in Python (13 Examples)
- Count Duplicates in List in Python (2 Examples)
- Learn Python Programming
This post has shown how to convert sets to lists in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter