Are there any reasons not to use an OrderedDict?

temporary_user_name

I'm referring to the OrderedDict from the collections module, which is an ordered dictionary.

If it has the added functionality of being orderable, which I realize may often not be necessary but even so, are there any downsides? Is it slower? Is it missing any functionality? I didn't see any missing methods.

In short, why shouldn't I always use this instead of a normal dictionary?

Tim Peters

OrderedDict is a subclass of dict, and needs more memory to keep track of the order in which keys are added. This isn't trivial. The implementation adds a second dict under the covers, and a doubly-linked list of all the keys (that's the part that remembers the order), and a bunch of weakref proxies. It's not a lot slower, but at least doubles the memory over using a plain dict.

But if it's appropriate, use it! That's why it's there :-)

How it works

The base dict is just an ordinary dict mapping keys to values - it's not "ordered" at all. When a <key, value> pair is added, the key is appended to a list. The list is the part that remembers the order.

But if this were a Python list, deleting a key would take O(n) time twice over: O(n) time to find the key in the list, and O(n) time to remove the key from the list.

So it's a doubly-linked list instead. That makes deleting a key constant (O(1)) time. But we still need to find the doubly-linked list node belonging to the key. To make that operation O(1) time too, a second - hidden - dict maps keys to nodes in the doubly-linked list.

So adding a new <key, value> pair requires adding the pair to the base dict, creating a new doubly-linked list node to hold the key, appending that new node to the doubly-linked list, and mapping the key to that new node in the hidden dict. A bit over twice as much work, but still O(1) (expected case) time overall.

Similarly, deleting a key that's present is also a bit over twice as much work but O(1) expected time overall: use the hidden dict to find the key's doubly-linked list node, delete that node from the list, and remove the key from both dicts.

Etc. It's quite efficient.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Are there any reasons why I should/shouldn't use ObjectId's in my RESTful url's

From Dev

Reasons not to use GROUP_CONCAT?

From Dev

What are the reasons NOT to use custom HTML tags?

From Dev

Are there any reasons to compile without optimizations?

From Dev

Poll vs. Push - Any reasons to avoid Push Notifications?

From Dev

Reasons why not to use WebAPI

From Dev

Are there any reasons to avoid using tmpnam() to get a name for a temporary file?

From Dev

Any reasons to use a a method instead of the class own destructor to clean up in C++?

From Dev

Good Reasons why not to use Iframes in page content

From Dev

How to use OrderedDict from DataStructures package in Julia?

From Dev

Is there any performance reasons to change a function into a static function?

From Dev

Any Other Reasons for Maven "Failed to retrieve plugin descriptor" Besides Proxy

From Dev

Are there any reasons to have an abstract class with every method in the class defined?

From Dev

Reasons to clear ccache or use multiple ccache directories

From Dev

Reasons to not use the default exchange on RabbitMQ?

From Dev

Reasons to use array.resize instead of redim

From Dev

Specific reasons to use |= instead of =

From Dev

Are there reasons not to use lombok with android studio

From Dev

Reasons to use Close over Dispose for a Stream?

From Dev

Reasons to use adaptive and energy saving power plans

From Dev

Reasons why not to use WebAPI

From Dev

Are there any reasons to not use async script elements?

From Dev

Are there any reasons I can't just use git to track changes to my svn checkout?

From Dev

Proper use of reasons

From Dev

Is there any performance reasons to change a function into a static function?

From Dev

Should use of application/x-httpd-php .html be avoided for any reasons?

From Dev

Practical Reasons to use C# "named arguments"

From Dev

Technical reasons to not use HTTP authentication

From Dev

Reasons not to use use a wildcard pull?

Related Related

  1. 1

    Are there any reasons why I should/shouldn't use ObjectId's in my RESTful url's

  2. 2

    Reasons not to use GROUP_CONCAT?

  3. 3

    What are the reasons NOT to use custom HTML tags?

  4. 4

    Are there any reasons to compile without optimizations?

  5. 5

    Poll vs. Push - Any reasons to avoid Push Notifications?

  6. 6

    Reasons why not to use WebAPI

  7. 7

    Are there any reasons to avoid using tmpnam() to get a name for a temporary file?

  8. 8

    Any reasons to use a a method instead of the class own destructor to clean up in C++?

  9. 9

    Good Reasons why not to use Iframes in page content

  10. 10

    How to use OrderedDict from DataStructures package in Julia?

  11. 11

    Is there any performance reasons to change a function into a static function?

  12. 12

    Any Other Reasons for Maven "Failed to retrieve plugin descriptor" Besides Proxy

  13. 13

    Are there any reasons to have an abstract class with every method in the class defined?

  14. 14

    Reasons to clear ccache or use multiple ccache directories

  15. 15

    Reasons to not use the default exchange on RabbitMQ?

  16. 16

    Reasons to use array.resize instead of redim

  17. 17

    Specific reasons to use |= instead of =

  18. 18

    Are there reasons not to use lombok with android studio

  19. 19

    Reasons to use Close over Dispose for a Stream?

  20. 20

    Reasons to use adaptive and energy saving power plans

  21. 21

    Reasons why not to use WebAPI

  22. 22

    Are there any reasons to not use async script elements?

  23. 23

    Are there any reasons I can't just use git to track changes to my svn checkout?

  24. 24

    Proper use of reasons

  25. 25

    Is there any performance reasons to change a function into a static function?

  26. 26

    Should use of application/x-httpd-php .html be avoided for any reasons?

  27. 27

    Practical Reasons to use C# "named arguments"

  28. 28

    Technical reasons to not use HTTP authentication

  29. 29

    Reasons not to use use a wildcard pull?

HotTag

Archive