Convert set into a list in Python
Last Updated :
30 Apr, 2025
In Python, sets are unordered collections of unique elements. While they're great for membership tests and eliminating duplicates, sometimes you may need to convert a set into a list to perform operations like indexing, slicing, or sorting. For example, if input set is {1, 2, 3, 4} then Output should be list - [1, 2, 3, 4]. Python offers multiple ways to convert a set to a list. Let’s explore them one by one with clear examples and explanations.
Using list() Constructor
The most direct and simplest way to convert a set to a list.
Python
a = {1, 2, 3, 4, 5}
b = list(a)
print(b)
Explanation: list() constructor takes any iterable, including a set, and returns a new list containing the same elements.
Using List Comprehension
List comprehension is a concise way to create a list from any iterable. While it is often used for more complex transformations, it can also be used to convert a set into a list.
Python
a = {1, 2, 3, 4, 5}
b = [item for item in a]
print(b)
Explanation: This method iterates over each item in the set and constructs a list out of it.
Using the * (Unpacking Operator)
Python’s unpacking operator * can also be used to unpack the elements of a set into a list. This method is effective and concise.
Python
a = {1, 2, 3, 4, 5}
b = [*a]
print(b)
Explanation: This technique creates a list by unpacking the elements of the set into a new list.
Using append() in a Loop
We can manually append each element from the set into a list using a for loop and the append() method. While this method is more verbose, it demonstrates control over how elements are added.
Python
a = {1, 2, 3, 4, 5}
b = []
for item in a:
b.append(item)
print(b)
Explanation: Although less efficient than the previous methods, this approach might be useful in situations where custom processing is needed while converting the set.
Using map() Function
The map() function applies a given function to each item in the iterable. In this case, the identity function (lambda x: x) can be used to map each element from the set to a list.
Python
a = {1, 2, 3, 4, 5}
b = list(map(lambda x: x, a))
print(b)
Explanation: Although this method is not as commonly used for simple conversions, it demonstrates how map() can be employed in this context.
Using copy() Method (Shallow Copy of Set to List)
We can convert a set to a list by creating a shallow copy of the set, although this approach is rarely used directly for set-to-list conversion.
Python
a = {1, 2, 3, 4, 5}
b = a.copy()
print(list(b))
Using extend() on an Empty List
We can use the extend() method to add elements from the set to an existing list (in this case, an empty list). This method is useful if we want to add elements to an already initialized list.
Python
a = {1, 2, 3, 4, 5}
b = []
b.extend(a)
print(b)
Related articles:
Similar Reads
Convert a Dictionary to a List in Python In Python, dictionaries and lists are important data structures. Dictionaries hold pairs of keys and values, while lists are groups of elements arranged in a specific order. Sometimes, you might want to change a dictionary into a list, and Python offers various ways to do this. How to Convert a Dict
3 min read
Convert a List to Dictionary Python We are given a list we need to convert the list in dictionary. For example, we are given a list a=[10,20,30] we need to convert the list in dictionary so that the output should be a dictionary like {0: 10, 1: 20, 2: 30}. We can use methods like enumerate, zip to convert a list to dictionary in pytho
2 min read
Python - Convert a list into tuple of lists When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists.For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this con
3 min read
Python | Convert list of tuples into digits Given a list of tuples, the task is to convert it into list of all digits which exists in elements of list. Letâs discuss certain ways in which this task is performed. Method #1: Using re The most concise and readable way to convert list of tuple into list of all digits which exists in elements of l
6 min read
Python | Convert list of tuples into list In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
3 min read