Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python the language does not define the order of the keys; any implementation can use any order it wants. <a href="http://docs.python.org/3/library/stdtypes.html#dictionary-view-objects" rel="nofollow">The documentation</a> says:</p> <blockquote> <p>Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.</p> </blockquote> <p>(The wording and location in the docs are slightly different in 2.x, but the idea is the same.)</p> <p>If you're asking specifically about the CPython implementation, the details have changed twice between early 2.x and 3.4, so you'd have to ask about a specific version, not CPython in general. And they're pretty complicated details, as Max Noel's answer implies.</p> <p>But really:</p> <ul> <li>It should never matter for almost any code.</li> <li>If it does matter, you probably want to read the source for the implementation(s) you care about, because most of them don't document it. For example, in CPython, see <a href="http://hg.python.org/cpython/file/9f38bbd4e041/Objects/dictobject.c" rel="nofollow">dictobject.c</a> (for the version you need).</li> </ul> <p>The one thing you <em>can</em> count on is that if you iterate the same dictionary repeatedly without changing it, the order will be consistent.</p> <p>If you want something that acts like a dictionary but preserves the insertion order of the keys, see <a href="http://docs.python.org/3/library/collections.html#collections.OrderedDict" rel="nofollow"><code>collections.OrderedDict</code></a> in the standard library. If you something that acts like a dictionary but keeps the keys in sorted order, you probably want a tree-based structure; there are multiple third-party options to choose from, like <a href="https://pypi.python.org/pypi/blist/" rel="nofollow"><code>blist.sorteddict</code></a> and <a href="https://pypi.python.org/pypi/bintrees/" rel="nofollow"><code>bintrees.FastRBTree</code></a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload