What is time complexity of a list to set conversion?

PythonListHashSetTime Complexity

Python Problem Overview


I've noticed the table of the time complexity of set operations on the python official website. But i just wanna ask what's the time complexity of converting a list to a set, for instance,

l = [1, 2, 3, 4, 5]
s = set(l)

I kind of know that this is actually a hash table, but how exactly does it work? Is it O(n) then?

Python Solutions


Solution 1 - Python

Yes. Iterating over a list is O(n) and adding each element to the hash set is O(1), so the total operation is O(n).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlxuechenView Question on Stackoverflow
Solution 1 - PythonMad PhysicistView Answer on Stackoverflow