pythontime-complexityordered-set

Time Complexity of OrderedSet() in python


I was going through this answer on Stack Overflow. I came to know about existence of OrderedSet in Python. I would like to know how it is implemented internally. Is it similar to hash table implementation of sets?

Also, what is the time complexity of some of the common operations like insert, delete, find, etc.?


Solution

  • From the documentation available here

    Implementation based on a doubly linked link and an internal dictionary. This design gives OrderedSet the same big-Oh running times as regular sets including O(1) adds, removes, and lookups as well as O(n) iteration.

    There is also a discussion on the topic, see Does Python have an ordered set?