1

The latest versions of Python from 3.7 on have a guarantee that the order of items in a dict will match the insertion order. So the following will always produce the same result:

>>> d = {}
>>> d['a'] = 1
>>> d['b'] = 2
>>> d['c'] = 3
>>> list(d.items())
[('a', 1), ('b', 2), ('c', 3)]

But when you define multiple keys at the same time, is the evaluation order guaranteed to be left to right? A quick test shows that it is, this one time, but is it something that can be relied on?

>>> d = {'z': 26, 'y': 25, 'x': 24}
>>> list(d.items())
[('z', 26), ('y', 25), ('x', 24)]

Something backed by a quote from the official Python documentation or a PEP would be best.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 2
    Does this answer your question? [Do Python dict literals and dict(list of pairs) keep their key order?](https://stackoverflow.com/questions/63873066/do-python-dict-literals-and-dictlist-of-pairs-keep-their-key-order) – jorf.brunning Jul 15 '21 at 20:33
  • This may also help [Are dictionaries ordered in Python 3.6+?](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6) – ThePyGuy Jul 15 '21 at 20:35
  • 1
    Indeed, the [accepted answer](https://stackoverflow.com/a/63876094/15873043) in the post linked by @jorf.brunning points to the [Python docs on expressions](https://docs.python.org/3/reference/expressions.html#dictionary-displays) that make it very clear: "When the comprehension is run, the resulting key and value elements are inserted in the new dictionary in the order they are produced." Edit: It also says "If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary", which might be more relevant here. – fsimonjetz Jul 15 '21 at 20:39
  • @ThePyGuy no it doesn't. I already understand and stated in the question that dictionaries will retain their order. The question is whether the order of a `dict` literal is well defined? – Mark Ransom Jul 15 '21 at 20:41
  • 1
    @fsimonjetz mmm well, for this question, the relevant section is "If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum. This means that you can specify the same key multiple times in the key/datum list, and the final dictionary’s value for that key will be the last one given." – juanpa.arrivillaga Jul 15 '21 at 20:42
  • @fsimonjetz but this question isn't about comprehensions, which have a `for` loop in them that implies an order. – Mark Ransom Jul 15 '21 at 20:44
  • 1
    @MarkRansom yeah, they quoted the wrong part, but read the part I quoted. Anyway, it's all pretty succinctly stated in the section on [dictionary displays](https://docs.python.org/3/reference/expressions.html#dictionary-displays) – juanpa.arrivillaga Jul 15 '21 at 20:50
  • @juanpa.arrivillaga You're right, I noticed afterwards and edited my answer just before you replied (see above)! – fsimonjetz Jul 15 '21 at 20:51
  • @juanpa.arrivillaga: chepner edited the accepted answer at the marked duplicate to include the first part of your text so now it really does answer my question. When first closed it did not. – Mark Ransom Jul 15 '21 at 21:00

1 Answers1

-1

The order of the elements will be the same as you set it, unless, of course, it is changed or sorted, this is unlikely to be in the documentation, but the elements are written one by one