Open In App

Python | Convert set into a list

Last Updated : 07 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a set, write a Python program to convert the given Set to List.

Input : ('Geeks', 'for', 'geeks')
Output : ['Geeks', 'for', 'geeks']
Explanation: The data type of the input is set <class 'set'>  and 
                        the data type of the output is list <class 'list'>.

Convert the set into a List in Python

Below are the methods to convert Set to List that we will cover below:

Convert Set to List using the list Method

Here we pass the set datatype inside the list parentheses as a parameter and this will convert the set data type into a list data type as shown in the code below.

Python3




# set into a list
my_set = {'Geeks', 'for', 'geeks'}
print(type(my_set))
 
s = list(my_set)
print(type(s))


Output:

['Geeks', 'for', 'geeks']

Time complexity: O(n)
Auxiliary Space: O(n)

Set into a List using the sorted() method

Using the sorted() function will convert the set into a list in a defined order. The only drawback of this method is that the elements of the set need to be sortable. 

Python3




# convert a set into a list
def convert(set):
    return sorted(set)
 
# Driver function
my_set = {1, 2, 3}
 
s = set(my_set)
print(convert(s))


Output:

[1, 2, 3]

Time complexity: O(n)
Auxiliary Space: O(n)

Convert the set into a list using the map() function

You can use the map() function to convert the set to a list by passing the set as an argument to the map() function and returning a list of the results. For example:

Python3




# program to convert a  set into a list
def convert(s):
    return list(map(lambda x: x, s))
# Driver function
s = {1, 2, 3}
print(convert(s))


Output:

[1, 2, 3]

Time complexity: O(n)
Auxiliary Space: O(n)

Convert Set to List using List Comprehension

You can use list comprehension to create a new list from the elements in the set as shown in the code below.

Python3




def convert(s):
    # Use a list comprehension to create a new list from the elements in the set
    return [elem for elem in s]
 
s = {1, 2, 3}
print(convert(s))


Output:

[1, 2, 3]

Time complexity: O(n)
Auxiliary Space: O(n)

Convert Set into a List using [*set, ]

This essentially unpacks the set s inside a list literal which is created due to the presence of the single comma (, ). This approach is a bit faster but suffers from readability. 

For example:

Python3




#program to convert a  set into a list
def convert(set):
    return [*set, ]
 
# Driver function
s = set({1, 2, 3})
print(convert(s))


Output:

[1, 2, 3]

Time complexity: O(n)
Auxiliary Space: O(n)

Convert Set to List using list() constructor

You can use the list() constructor to convert a set to a list directly, In below code we creates a set `my_set` with elements 1 to 5, then converts it into a list `my_list` using the `list()` constructor, and prints the resulting list. The output is `[1, 2, 3, 4, 5]`.

Python3




my_set = {1, 2, 3, 4, 5}
my_list = list(my_set)
print(my_list)


Output :

[1, 2, 3, 4, 5]

Time complexity: O(n)
Auxiliary Space: O(n)

Convert Set to List using copy and clear

You can make a copy of the set and then clear the set to get an empty set, which can be used to create an empty list. As the below code creates a set `my_set` with elements 6 to 10, makes a copy of the set as a list `my_list`, clears the original set, and prints the copied list. The output is `[6, 7, 8, 9, 10]`.

Python3




my_set = {6, 7, 8, 9, 10}
my_list = list(my_set.copy())
my_set.clear()
print(my_list)


Output :

[6, 7, 8, 9, 10]

Time complexity: O(n)
Auxiliary Space: O(n)



Similar Reads

Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read
Python | Convert a string representation of list into list
Many times, we come across the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to a list in Python to perform tasks is quite common in web development. Convert a string of a list in
6 min read
Python | Convert list of string into sorted list of integer
Given a list of string, write a Python program to convert it into sorted list of integer. Examples: Input: ['21', '1', '131', '12', '15'] Output: [1, 12, 15, 21, 131] Input: ['11', '1', '58', '15', '0'] Output: [0, 1, 11, 15, 58] Let's discuss different methods we can achieve this task. Method #1: Using map and sorted() C/C++ Code # Python code to
4 min read
Python | Convert list of tuples into list
Given a list of tuples, write a Python program to convert it into a list. Example Input: [(11, 20), (13, 40), (55, 16)] Output:[11, 20, 13, 40, 55, 16] Input: [('Geeks', 2), ('For', 4), ('geek', '6')] Output: ['Geeks', 2, 'For', 4, 'geek', '6'] Convert List of Tuples to List of Lists in Python MethodsConvert lists of tuples into a list using LoopTh
5 min read
Python | Convert list into list of lists
Given a list of strings, write a Python program to convert each element of the given list into a sublist. Thus, converting the whole list into a list of lists. Examples: Input : ['alice', 'bob', 'cara'] Output : [['alice'], ['bob'], ['cara']] Input : [101, 202, 303, 404, 505] Output : [[101], [202], [303], [404], [505]] Approach #1 : Naive Approach
5 min read
Python | Ways to Convert a 3D list into a 2D list
List is a common type of data structure in Python. While we have used the list and 2d list, the use of 3d list is increasing day by day, mostly in case of web development. Given a 3D list, the task is to convert it into a 2D list. These type of problems are encountered while working on projects or while contributing to open source. Below are some w
3 min read
Python program to convert a list into a list of lists using a step value
Given a List, the task here is to write a python program that can split the list into list of lists using by a step value here denoted via K. Input : test_list = [5, 6, 3, 2, 7, 1, 9, 10, 8], K = 3Output : [[5, 2, 9], [6, 7, 10], [3, 1, 8]]Explanation : 5, 2 and 9 are 0th, 3rd and 6th element respectively, and hence ( difference 3 ) grouped togethe
4 min read
Python | Convert given list into nested list
Sometimes, we come across data that is in string format in a list and it is required to convert it into a list of the list. This kind of problem of converting a list of strings to a nested list is quite common in web development. Let's discuss certain ways in which this can be performed. Convert the Given List into Nested List in PythonBelow are th
5 min read
How to Convert a List into Ordered Set in Python?
One such data structure that often proves useful is the ordered set. An ordered set combines the characteristics of both a set and a list, ensuring the uniqueness of elements while preserving the order in which they were added. In this article, we will explore the concept of an ordered set and some different methods for converting a list into this
4 min read
Convert a List of Dictionaries into a Set of Dictionaries
Python's versatility allows developers to manipulate data in various ways. When working with a list of dictionaries, there might be scenarios where you want to convert it into a set of dictionaries to eliminate duplicates or for other reasons. In this article, we'll explore three different methods to achieve this goal with code examples. Convert A
3 min read
three90RightbarBannerImg