news.volyx.in

Python dicts are now ordered (softwaremaniacs.org)

734 points by signa11 · 2438 days ago · 436 comments on HN

Article summary

Python dictionaries are now guaranteed to maintain their insertion order, starting from version 3.7. This change was initially an implementation detail in CPython from version 3.6, but has now become a standard feature. The ordered behavior is a result of a new internal data structure that stores keys and values in a separate dense array. This change brings Python on par with other languages like PHP and Ruby, which also have ordered maps or dictionaries.

Main themes

  • ordered dictionaries
  • Python 3.7
  • language design
  • backward compatibility
  • code readability
  • implementation details

What commenters say

  • The ordering of dictionaries is a useful feature, but it can also lead to code that relies on this behavior and breaks when run on earlier versions of Python.
  • Some languages, like Go, intentionally randomize the order of map iterations to prevent code from relying on a specific order.
  • The decision to make dictionaries ordered is seen as a major breaking change by some, while others argue that it is a useful feature that opens up new possibilities.
  • Code that assumes a specific order of dictionary items is already broken, as the order was never guaranteed in the first place.
  • The ordered behavior of dictionaries can be useful in certain situations, such as when parsing JSON files and preserving the original order of attributes.
  • The use of ordered dictionaries can make code more readable and easier to understand, but it can also lead to subtle bugs if not used carefully.