Time Complexity to Convert a Set to List in Python
Last Updated :
09 Feb, 2024
Concerting a set to a list involves iterating through the elements of the set and adding them to a list. The time complexity of this operation is linear and depends on the size of the set. Let’s denote the size of the set as n. The time complexity to convert a set to a list generally takes O(n) time. where n is the n of elements in the set. There are various methods to convert a set to a list in Python. In this article, we will learn about the time complexity of different ways to convert a set to a list in Python.
Time Complexity to Convert a Set to List in Python
Below are some of the ways by which we can convert a set to a list in Python in different time complexity scenarios in Python:
Using list() constructor
In this method, we are going to use an in-built list() constructor which converts a set to a list directly. we have nothing to do with the constructor just call it and pass the set to it.
Python3
import time
small_set = { 1 , 2 , 3 , 4 , 5 }
start = time.time()
small_list = list (small_set)
print ( "List is:" ,small_list)
end = time.time()
print ( "Small Set to List Time:" , round (end - start, 5 ))
large_set = set ( range ( 1 , 1000001 ))
start_time = time.time()
large_list = list (large_set)
end_time = time.time()
print ( "\nLarge Set to List Time:" , round (end_time - start_time, 5 ))
|
Output
List is: [1, 2, 3, 4, 5]
Small Set to List Time: 2e-05
Large Set to List Time: 0.0148
Using extend() method
The extend() method use to add the specified list elements (or any iterable) to the end of the current list. so in this method we are going to use this in-built provided functionality of the list.
Python3
import time
s_set = { 1 , 2 , 3 , 4 , 5 }
start = time.time()
my_list = []
my_list.extend(s_set)
end = time.time()
print ( "List is : " ,my_list)
print ( "Small Set to List Time in sec:" , round (end - start, 5 ))
l_set = set ( range ( 1 , 1000001 ))
start = time.time()
my_list2 = []
my_list2.extend(l_set)
end = time.time()
print ( "\nLarge Set to List Time in sec:" , round (end - start, 6 ))
|
Output
List is : [1, 2, 3, 4, 5]
Small Set to List Time in sec: 0.0
Large Set to List Time in sec: 0.011127
Using List Comprehension
List comprehension is the short way for doing a task. And we can also use list comprehension for conversion of list to set. In this method we are iterating through the set and inserting the element of set to a list.
Python3
import time
s_set = { 1 , 2 , 3 , 4 , 5 }
start = time.time()
my_list = [i for i in s_set]
end = time.time()
print ( "Newly Created list : " , my_list)
print ( "Small Set to List Time:" , round (end - start, 5 ))
l_set = set ( range ( 1 , 1000001 ))
start = time.time()
my_list2 = [item for item in l_set]
end = time.time()
print ( "\nLarge Set to List Time:" , round (end - start, 6 ))
|
Output
Newly Created list : [1, 2, 3, 4, 5]
Small Set to List Time: 0.0
Large Set to List Time: 0.02328
Conclusion
The time complexity of this operation is O(n), where n is the number of elements in the set. Each element in the set needs to be added to the list, and this process takes linear time.
Share your thoughts in the comments
Please Login to comment...