How to convert a set to a list Python [6 + Examples]

In this Python tutorial, I will discuss how to convert a set to a list Python. We’ll talk about the procedures and methods present to convert the set to a list in Python. I have also explained how to convert frozenset to list in Python.

Recently, I was working on a Python Project where we got one requirement that we have to store unique numbers so far this is the reason we used the Python set. But in cases, if we want to change the number in that case we need those numbers in the Python list.

We’ll go over easy techniques that I came across during my research, which will make it easy to change a Python set object into a List object.

Sets are the iterable and mutable unordered collection of data types in Python.

Python uses lists to store the sequential order of different kinds of data. Because Python lists are mutable types, we can change their elements after they’ve been generated.

Methods to Convert a set to a list Python

There are numerous ways to convert a set to a list Python.

  • The list() Constructor
  • Using a For Loop
  • Using List Comprehension
  • The * unpacking operator
  • The sorted() function
  • The map() function

Let’s see them one by using some demonstrative examples:

Note: However, since Python sets don’t have a fixed order, the resulting Python list‘s order might not always be the same across different conversions.

Method 1: Convert set to list Python using list() constructor

The list() constructor is a built-in tool in Python meant for creating lists. When we wrap a Python set inside the list() function, it reads each element from the set and places it into a new list in Python.

Example: Suppose we have a set of data. Now, we want to convert this set into a list through Python for further processing.

states_set = {"California", "Washington", "Texas"}
states_list = list(states_set)
print(states_list)
print(type(states_list))

Output: Here, the list() function creates a new list in Python with the elements found in the Python set. The order is not guaranteed since sets in Python are unordered by nature. The output might be different from now the set in Python is looking.

['Washington', 'Texas', 'California']
<class 'list'>
convert a set to a list python

The list() constructor is the easiest way to convert a set to a list Python.

Method 2: Python set to list using a for loop

A for loop can be thought of as a manual process where we go through each item in a Python set, one by one, and place it inside a new list in Python.

Scenario: We have a set of data and want to compile them into a list and also add a suffix for clarity through Python.

coastal_states_set = {"Oregon", "Alaska", "Hawaii"}
coastal_states_list = []
for state in coastal_states_set:
    coastal_states_list.append(state + " (Coastal)")
print(coastal_states_list)
print(type(coastal_states_list))

Output: The for loop manually iterates over each element in the Python set, appending each element followed by the ” (Coastal)” suffix (additional condition) to a new list in Python.

['Oregon (Coastal)', 'Hawaii (Coastal)', 'Alaska (Coastal)']
<class 'list'>
set to list python

This way we can use for loop and the square brackets [] to convert a set to a list Python.

Method 3: How to convert a set into list in Python using list comprehension

List comprehension is a compact way of creating lists by iterating over existing iterables in Python.

Example: Imagine we want to add a prefix to each element of a set in Python. But, couldn’t do it as the set is Partially mutable, so need to convert it into a Python list.

states_set = {"Florida", "Nevada", "Maine"}
states_list = ["State of " + state for state in states_set]
print(states_list)
print(type(states_list))

Output: The list comprehension iterates over each element in the Python set, adds prefixes to it, and converts it into a list.

['State of Florida', 'State of Nevada', 'State of Maine']
<class 'list'>
convert set to list Python

This way we can use list comprehension to convert a set to a list Python.

Method 4: Python convert set to list using the * (unpacking) function

When the * (unpacking) operator is used within a list in Python, it takes each element from a Python set and “unpacks” them into the list.

Scenario: Let’s say we have a set of items in Python and we have to convert them into a list through Python.

priority_states_set = {"New York", "New Jersey", "Connecticut"}
consolidated_list = [*priority_states_set, "Georgia", "Alabama"]
print(consolidated_list)
print(type(consolidated_list))

Output: The * operator unpacks each item from Python set into list in Python followed by some pre-stored data in the list.

['Connecticut', 'New Jersey', 'New York', 'Georgia', 'Alabama']
<class 'list'>
how to convert a set to list in python

This way we can unpack a set inside a Python list using the * operator to convert a set to a list Python.

Method 5: Convert a set to list in Python using sorted() function.

The sorted() function is like a machine that takes in an iterable, rearranges its items in ascending order, and then gives out a list in Python. When we feed a Python set into this, it outputs a Python list with the set’s elements, but in a sorted manner.

Example: We have a set of information, and we want to display it in alphabetical order but in the form of a Python list.

survey_states_set = {"Ohio", "Michigan", "Wisconsin"}
sorted_states_list = sorted(survey_states_set)
print(sorted_states_list)
print(type(sorted_states_list))

Output: The sorted() function returns a new list containing the states from Python set in alphabetical order.

['Michigan', 'Ohio', 'Wisconsin']
<class 'list'>
transform set to list python

This way the sorted() function is used to convert a set to a list Python.

Method 6: Convert a set to list Python using the map() function

The map() function is where each item from an iterable (like a set) is processed through a function in Python. In our use case, we’re merely passing each item of the Python set through a function that does nothing (a trivial lambda function) and returns the item unchanged.

Example: We have a set of states, and for documentation purposes, we want to wrap each state name in square brackets.

document_states_set = {"Illinois", "Indiana", "Iowa"}
bracketed_states_list = list(map(lambda state: f"[{state}]", document_states_set))
print(bracketed_states_list)
print(type(document_states_set))

Output: The map() function applies the lambda function in Python to each item in the Python set, wrapping them in square brackets as a Python list.

['[Iowa]', '[Illinois]', '[Indiana]']
<class 'set'>
how to convert set into list in python

The map() function is used to convert a set to a list in Python.

Convert frozenset to list in Python

A frozenset is an immutable version of a regular set in Python, meaning its contents cannot be modified after it’s created. However, just like with regular sets, converting a frozenset to a list is straightforward in Python.

Example: Using the list() Constructor to convert frozenset to list in Python.

frozen = frozenset([1, 2, 3, 4, 5])
converted_list = list(frozen)
print(converted_list)
print(type(converted_list))

Output: The list() constructor can accept any iterable, and since a frozenset is iterable, it can be passed directly to list(). This method creates a new list containing all the elements of the frozenset.

[1, 2, 3, 4, 5]
<class 'list'>
how to convert a set to a list in python using frozenset()

This way we can have a frozenset and then convert that into a list in Python using the list() constructor.

Note: All other methods we discussed previously for sets (like list comprehensions, unpacking, etc.) can be applied similarly to frozensets.

Conclusion

This Python article explains how to convert the set to a list Python using methods like the list() constructor, using a for loop, using list comprehension, the * unpacking operator, the sorted() function, and the map() function with the help of some examples. I have also explained how to convert frozenset to list in Python.

You may also like to read the following Python tutorials.