Time Complexity of A List to Set Conversion in Python
Last Updated :
09 Feb, 2024
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 will compare time complexities for different conversion from a list to a set in Python.
Comparing Time of List To Set Conversion
Below are some of the comparisons for the time complexity of the list to set conversion in Python:
Using set() Constructor
In this example, the script measures the time taken to convert a small list (`small_list`) and a large list (`large_list`) to sets using the `set()` constructor. The execution time is calculated using the `time` module, showcasing the efficiency of the set conversion process for both small and large datasets.
Python3
import time
# For Small List
small_list = [1, 2, 3, 4, 5]
start = time.time()
small_set = set(small_list)
end = time.time()
print("The time to convert small list to set : ", round(end - start, 5))
print("The small set is : ", small_set)
# For Large List
large_list = list(range(1, 1000001))
start = time.time()
large_set = set(large_list)
end = time.time()
print("The time to convert large list to set : ", round(end - start, 5))
OutputThe time to convert small list to set : 0.0
The small set is : {1, 2, 3, 4, 5}
The time to convert large list to set : 0.0527
Using set() with unpacking
In this example, the script measures the time taken to convert a small list (`small_list`) and a large list (`large_list`) to sets using the `set()` constructor with the unpacking syntax (`*`). However, the usage of `*` in `set(*[small_list])` and `set(*[large_list])` is unnecessary and equivalent to `set(small_list)` and `set(large_list)`. The execution time is calculated using the `time` module, demonstrating the efficiency of the set conversion process for both small and large datasets.
Python3
import time
# For Small List
small_list = [1, 2, 3, 4, 5]
start = time.time()
small_set = set(*[small_list])
end = time.time()
print("The time to convert small list to set : ",round(end - start,5))
print("The set is : ",small_set)
# For Large List
large_list = list(range(1, 1000001))
start = time.time()
large_set = set(*[large_list])
end = time.time()
print("The time to convert large list to set : ",round(end - start,5))
OutputThe time to convert small list to set : 0.0
The set is : {1, 2, 3, 4, 5}
The time to convert large list to set : 0.05134
Using update method
In this example, the script measures the time taken to convert a small list (`small_list`) and a large list (`large_list`) to sets using the `update()` method of the `set` data structure. The `update` method efficiently incorporates the elements from the lists into the sets, and the execution time is calculated using the `time` module. The resulting sets, `small_set` and `large_set`, contain unique elements from the corresponding lists.
Python3
import time
# For Small List
small_list = [1, 2, 3, 4, 5]
small_set = set()
start = time.time()
small_set.update(small_list)
end = time.time()
print("The time to convert small list to set : ", round(end - start,5))
print("The set is : ", small_set)
# For Large List
large_list = list(range(1, 1000001))
large_set = set()
start = time.time()
large_set.update(large_list)
end = time.time()
print("The time to convert large list to set : ", round(end - start, 5))
OutputThe time to convert small list to set : 0.0
The set is : {1, 2, 3, 4, 5}
The time to convert large list to set : 0.05066
Using Loops and add() method of set
In this example, the script measures the time taken to convert a small list (`small_list`) and a large list (`large_list`) to sets using a `for` loop and the `add` method of the `set` data structure. The `add` method efficiently incorporates the elements from the lists into the sets, and the execution time is calculated using the `time` module. The resulting sets, `small_set` and `large_set`, contain unique elements from the corresponding lists. This approach offers explicit control over the set creation process, particularly useful for scenarios where customization is needed during the conversion.
Python3
import time
# For Small List
small_list = [1, 2, 3, 4, 5]
small_set = set()
start = time.time()
for element in small_list:
small_set.add(element)
end = time.time()
print("The time to convert small list to set : ", round(end - start, 5))
print("The set is : ", small_set)
# For Large List
large_list = list(range(1, 1000001))
large_set = set()
start = time.time()
for element in large_list:
large_set.add(element)
end = time.time()
print("The time to convert large list to set : ", round(end - start, 5))
OutputThe time to convert small list to set : 0.0
The set is : {1, 2, 3, 4, 5}
The time to convert large list to set : 0.21737
Conclusion
The time complexity of list to set conversion is O(n) where n is the number of element in the list. This is because element in the list needs to be processed to determine its uniqueness in the set. There are various way as we seen there is set() constructor, loops and add() method, using update method() or we can use set() with unpacking, developers have a range of efficient options. The choice of method depends on specific requirements and preferences, allowing for flexibility in managing set to list .
Similar Reads
Time Complexity to Convert a Set to List in Python 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
3 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 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
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
Creating a List of Sets in Python Creating a list of sets in Python involves storing multiple sets within list structure. Each set can contain unique elements and the list can hold as many sets as needed. In this article, we will see how to create a list of set using different methods in Python.Using List ComprehensionList comprehen
2 min read
Python - Convert List of lists to list of Sets We are given a list containing lists, and our task is to convert each sublist into a set. This helps remove duplicate elements within each sublist. For example, if we have:a = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]The output should be:[{1, 2}, {1, 2, 3}, {2}, {0}]Let's explore some method's to ac
2 min read