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.
[GFGTABS]
Python
a = {1, 2, 3, 4, 5}
b = list(a)
print(b)
[/GFGTABS]
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.
[GFGTABS]
Python
a = {1, 2, 3, 4, 5}
b = [item for item in a]
print(b)
[/GFGTABS]
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.
[GFGTABS]
Python
a = {1, 2, 3, 4, 5}
b = [*a]
print(b)
[/GFGTABS]
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.
[GFGTABS]
Python
a = {1, 2, 3, 4, 5}
b = []
for item in a:
b.append(item)
print(b)
[/GFGTABS]
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.
[GFGTABS]
Python
a = {1, 2, 3, 4, 5}
b = list(map(lambda x: x, a))
print(b)
[/GFGTABS]
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.
[GFGTABS]
Python
a = {1, 2, 3, 4, 5}
b = a.copy()
print(list(b))
[/GFGTABS]
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.
[GFGTABS]
Python
a = {1, 2, 3, 4, 5}
b = []
b.extend(a)
print(b)
[/GFGTABS]
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 co
3 min read
Convert Python List to Json
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the json module provides a convenient way to work with JSON data. In this article, we'll explore how to convert Python lists t
3 min read
Convert List Of Dictionary into String - Python
In Python, lists can contain multiple dictionaries, each holding key-value pairs. Sometimes, we need to convert a list of dictionaries into a single string. For example, given a list of dictionaries [{âaâ: 1, âbâ: 2}, {âcâ: 3, âdâ: 4}], we may want to convert it into a string that combines the conte
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
Convert List to Tuple in Python
The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
2 min read
Convert Set of Tuples to a List of Lists in Python
Sets and lists are two basic data structures in programming that have distinct uses. It is sometimes necessary to transform a collection of tuples into a list of lists. Each tuple is converted into a list throughout this procedure, and these lists are subsequently compiled into a single, bigger list
3 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
Convert Dict of List to CSV - Python
To convert a dictionary of lists to a CSV file in Python, we need to transform the dictionary's structure into a tabular format that is suitable for CSV output. A dictionary of lists typically consists of keys that represent column names and corresponding lists that represent column data.For example
4 min read