Time Complexity of A List to Set Conversion in Python (original) (raw)

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))

`

Output

The 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))

`

Output

The 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))

`

Output

The 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))

`

Output

The 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 .