简体   繁体   中英

How does OrderedDict keep things in Order in Python

I'm curious as to how OrderedDict from the collections library keeps key/pair order? I looked around online and couldn't find an answer.

From the source code, it appears to be implemented as a dict with a doubly linked list of keys for ordering, as well as another dict that maps keys to their position in the list.

  • Insertion just adds to the end of the list.
  • Deletion uses the second dict to remove an element from the list.
  • Iteration iterates over the linked list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM