Convert a Set to a List in Python

To convert a set to a list in Python, you can use the list() constructor, which takes an iterable (such as a set) and converts it into a list. This process is useful when you need an ordered and indexable collection from a set.


Examples

1. Convert a Set to a List Using the list() Constructor

The simplest way to convert a set to a list is by using the list() function, which automatically converts each element of the set into an element of a list.

</>
Copy
# Creating a set
num_set = {1, 2, 3, 4, 5}

# Converting set to list
num_list = list(num_set)

# Printing the list
print("Converted List:", num_list)

Explanation:

Here, we define a set num_set containing unique numerical values. The list() constructor is then used to convert num_set into a list named num_list. Since sets are unordered collections, the resulting list might not retain the original set’s order.

Output:

Converted List: [1, 2, 3, 4, 5]

2. Using a Loop to Convert a Set to a List

Instead of using list(), we can manually iterate over the set and append each element to a new list.

</>
Copy
# Creating a set
char_set = {'a', 'b', 'c', 'd'}

# Creating an empty list
char_list = []

# Adding each element from set to list
for char in char_set:
    char_list.append(char)

# Printing the list
print("Converted List:", char_list)

Explanation:

We initialize a set char_set containing string elements. An empty list char_list is created, and we use a for loop to iterate over each element in the set and append it to the list. This approach gives more control over element addition.

Output:

Converted List: ['a', 'b', 'c', 'd']

3. Converting a Set to a Sorted List

To maintain a specific order when converting a set to a list, we can use the sorted() function.

</>
Copy
# Creating a set
num_set = {5, 3, 1, 4, 2}

# Converting set to a sorted list
sorted_list = sorted(num_set)

# Printing the sorted list
print("Sorted List:", sorted_list)

Explanation:

Here, the set num_set contains unordered numbers. The sorted() function converts the set into a list and arranges its elements in ascending order, storing the result in sorted_list.

Output:

Sorted List: [1, 2, 3, 4, 5]

4. Convert a Set to a List Using List Comprehension

List comprehension offers a concise way to iterate through a set and create a list.

</>
Copy
# Creating a set
word_set = {"hello", "world", "python"}

# Converting set to list using list comprehension
word_list = [word for word in word_set]

# Printing the list
print("Converted List:", word_list)

Explanation:

We define a set word_set containing words. The list comprehension [word for word in word_set] iterates through each element of the set and adds it to a new list called word_list, achieving the same result as using a loop but in a single line.

Output:

Converted List: ['hello', 'python', 'world']

Conclusion

Converting a set to a list in Python is straightforward and can be done using different methods:

  1. Using list(): The simplest and most common approach.
  2. Using a Loop: Manually iterating using a For loop and appending each element to a list.
  3. Using sorted(): Converts and sorts the set in one step.
  4. Using List Comprehension: A concise way to iterate and create a list.