I know that the python dict is smaller, but why do I get the keys in the same order "always",
I have a dict named tmp :
>>> tmp {1: 'ONE', 2: 'TWO', 3: 'THREE'} >>> type(tmp) <type 'dict'> I repeated this dict twenty times, and in all iterations I got the list of keys in the same order.
>>> for i in range(20): ... print tmp.keys() ... [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3] I know that the python dict is smaller , but why do I always get the keys in the same order " ?
You do not change tmp between calls to tmp.keys() , so the order of the keys never changes.
If
items(),keys(),values(),iteritems(),iterkeys()anditervalues()are called without intermediate changes to the dictionary, the lists will correspond directly. This allows you to create pairs(value, key)usingzip():pairs = zip(d.values(), d.keys()). The same relation holds for theiterkeys()anditervalues()methods:pairs = zip(d.itervalues(), d.iterkeys())provides the same value for pairs. Another way to create the same list ispairs = [(v, k) for (k, v) in d.iteritems()].
When creating a dict, python creates its own order. Each time you call .keys() or .values() or .items() , you get the result unordered in the same order every time you change the dictionary. Hope this makes sense.
As you can see in the example below, although I am creating a dictionary with a specific order, python reorders the dictionary. However, after that, the order always remains unchanged (until you change the dictionary).
a = {'apple' : 'fruit', 'car' : 'vehicle', 'onion' : 'vegetable'} print a for i in range(5): print a.keys() [OUTPUT] {'car': 'vehicle', 'apple': 'fruit', 'onion': 'vegetable'} ['car', 'apple', 'onion'] ['car', 'apple', 'onion'] ['car', 'apple', 'onion'] ['car', 'apple', 'onion'] ['car', 'apple', 'onion'] Because otherwise it will not work:
for key, value in zip(d.keys(), d.values()): ... And various similar problems.
Python types and types are guaranteed to have the same iteration order until the dict / set parameter is changed. Try adding other values ββto tmp , and most likely next time you will get a different order.
(However, note that small integers as keys are not a particularly wonderful example of how this works - small integer hashes on their own in CPython, so they are likely to pop up numerically. Try a few lines.)
Since the python dict is implemented as a Hash Table, the key order is based on its hash value, which determines which slot is used to store the key / value pair.
For an integer value as a key, the hash value is just an integer
>>> map(hash, [1,2,3]) [1,2,3] Here, what you use as a key is a sequential int, their hash value is also sequential, there is a high probability that the lower hash algorithm selects consecutive slots for storing key / value pairs, so you see the same order that and you create a dict. Here is a simple counterexample:
>>> tmp = {1:1, 5:5, 10:10} >>> tmp.keys() [1, 10, 5] And, of course, during the loop you do not change the dictionary, which will not lead to a change in the size of the hash table, so the order will remain.
>>> for i in range(5): ... print tmp.keys() [1, 10, 5] [1, 10, 5] [1, 10, 5] [1, 10, 5] [1, 10, 5] If you are interested in a detailed implementation in Python, check out this good article: Implementing a Python Dictionary