Python | Convert set into a list
Last Updated :
07 Dec, 2023
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
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
def convert( set ):
return sorted ( set )
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
def convert(s):
return list ( map ( lambda x: x, s))
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):
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
def convert( set ):
return [ * set , ]
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)
Please Login to comment...