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
# Method - 1
import time
# for small set
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))
# for Large set
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))
OutputList 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
# Method - 2
import time
# For small set
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))
# For large set
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))
OutputList 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
# Method - 3 Using List Comprehension
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))
# For large set
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))
OutputNewly 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.
Similar Reads
Time Complexity of A List to Set Conversion in Python The time complexity of converting a list to a set is predominantly determined by the underlying hash function used by the set data structure. The average-case time complexity is O(n), where n is the number of elements in the list. In this article, we will see compare different scenarios in which we
5 min read
Time Complexity for Adding Element in Python Set vs List Adding elements differs due to their distinct underlying structures and requirements for uniqueness or order. Let's explore and compare the time complexity by adding elements to lists and sets. In this article, we will explore the differences in time complexities between a Set and a List if we add a
2 min read
Convert set into a list in Python 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 shoul
3 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 Set To List Without Changing Order Sets in Python are inherently unordered collections. If the order of the elements is important, the input set must have been created in a specific order, such as by using an ordered iterable like a list. Let us explore different methods to convert a set to a list without changing its apparent order.
2 min read