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
Python | Convert column to separate elements in list of lists
There are instances in which we might require to extract a particular column of a Matrix and assign its each value as separate entity in list and this generally has a utility in Machine Learning domain. Let's discuss certain ways in which this action can be performed.Method #1 : Using list slicing a
4 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 comprehe
2 min read
Python | List of tuples to dictionary conversion
Interconversions are always required while coding in Python, also because of the expansion of Python as a prime language in the field of Data Science. This article discusses yet another problem that converts to dictionary and assigns keys as 1st element of tuple and rest as it's value. Let's discuss
3 min read
Python - Convert List of Integers to a List of Strings
We are given a list of integers and our task is to convert each integer into its string representation. For example, if we have a list like [1, 2, 3] then the output should be ['1', '2', '3']. In Python, there are multiple ways to do this efficiently, some of them are: using functions like map(), re
3 min read
Python | Dictionary to list of tuple conversion
Inter conversion between the datatypes is a problem that has many use cases and is usual subproblem in the bigger problem to solve. The conversion of tuple to dictionary has been discussed before. This article discusses a converse case in which one converts the dictionary to list of tuples as the wa
5 min read
How to Convert a List into Ordered Set in Python?
The task is to turn a list into an ordered set, meaning we want to remove any duplicate items but keep the order in which they first appear. In this article, we will look at different ways to do this in Python. Using OrderedDict.fromkeys()OrderedDict.fromkeys() method removes duplicates from a list
2 min read