4
dict_mark = {'Wang': 'C', 'Li': 'B', 'Ma': 'A'}
s = ''
for c in dict_mark.values():
    s += c
print(s)

dictionary is unordered,so why does dict_mark.values() always return this value sequence like 'C' 'B' 'A'?

Why not 'B' 'A' 'C' or 'A' 'B' 'C'?

Chen A.
  • 10,140
  • 3
  • 42
  • 61
Bob liao
  • 569
  • 4
  • 15
  • 5
    `Dict` stores values in a hashtable. Read how hashtables works. Basically dict data is unordered because you can't guarntee the order of items; that's because how hashtable works – Chen A. Sep 22 '17 at 08:48
  • 4
    It's also good to know that the ways this works differs between Python implementations and even versions. – Klaus D. Sep 22 '17 at 08:51

5 Answers5

5

Unordered dos not mean not deterministic.

From the python 2.x docs:

If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.

In python 3.x docs:

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.

Thus, the sequence returned is always the same until you modify the dictionary. You just can't make assumptions on the objects being sorted in it.

Have a look at this question and the answers to it where they discuss about why (and how) in python 3.6+ dicts are actually ordered.

GPhilo
  • 18,519
  • 9
  • 63
  • 89
0

Since Python 3.6, dict keywords are ordered: https://mail.python.org/pipermail/python-dev/2016-September/146327.html

Pirheas
  • 173
  • 2
  • 7
0

dict stores it's data in a hashtable data structure. So how does hashtable works?

On a nutshell: let's say the dict is initalized to 8 slots array-like object. When you add a new key-value pair to the dict, it hashes the key using a function that returns the slot the key will reside. This isn't deterministic since the slot may be already taken; so you need to re-evaluate and look for another slot.

That's why the order you retrieve values from dict.values() changes depend on the data in it. That's why it is called unordered.

For example, consider this simple dict:

>>> d = {'a': 1, 'aa': 2}
>>> d
{'a': 1, 'aa': 2}

>>> d = {'aa': 1, 'a': 2}
>>> d
{'aa': 1, 'a': 2}

If I change the order of the keys, it also appears different when I print the dict key-value pairs. But look what happens if I use different key

>>> d = {'b': 1, 'a': 2}
>>> d
{'a': 2, 'b': 1}

Althought I stated the 'b' key first, it was allocated after 'a'.

However, once the dict is set, it will always return the same order when called dict.items().

Chen A.
  • 10,140
  • 3
  • 42
  • 61
0

The dictionary is stored as a hash table. In versions of Python prior to 3.6 iteration is done in the order they appear in the hash table, so that means the order you get depends on the hash value of each key. It also can vary based on the order of insertion and deletion when there have been hash conflicts.

In Python 2.x the hash value for a string is a fixed value, so while it might change between Python versions you will always see the same order for a given dictionary and a fixed set of actions on the dictionary.

In some versions of Python 3, the hash value for a string has random factor, so different runs will give a different result. In Python 3.6 the iteration order no longer depends on the hash key so you will again get a fixed order which will relate to the order of insertion (but there's no guarantee that it won't change again in the future).

$ python2.7 /tmp/t.py
ACB
$ python2.7 /tmp/t.py
ACB
$ python3.5 /tmp/t.py
BCA
$ python3.5 /tmp/t.py
ABC
$ python3.5 /tmp/t.py
CBA
Duncan
  • 92,073
  • 11
  • 122
  • 156
-2

It depends on several things in python how dict is ordered. You shouldn't think about it, since ordering in dict doesn't matter.

JJAACCEeEKK
  • 194
  • 1
  • 11
  • 1
    "You shouldn't wonder about that" is an appropriate answer if you actually provide the answer and then explain why it's irrelevant. As it happens, there are indeed scenarios in which maintaining insertion order in a dictionary can be useful – GPhilo Sep 22 '17 at 08:49
  • Have a look at [this question](https://stackoverflow.com/questions/39980323/dictionaries-are-ordered-in-python-3-6) for examples of why dict odering is actually pretty useful – GPhilo Sep 22 '17 at 08:52