-2

trying to understand what is going on here with a python dictionary. I am trying to return a list of the items using dic.values(). However, I can't figure out the order that items are supposed to be returned in.

For example:

dic = {'a':10, 'b':9, 'c':11, 'd':12}
>>> dic.values()
[10, 11, 9, 12]

Returns not one of the logical options of sorting by keys or sorting by items, but something else entirely

user3684792
  • 2,542
  • 2
  • 18
  • 23

1 Answers1

3

There is no "logical" ordering in a Python set. It is normally printed by order of hash which is supposed to be arbitrary.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • `s/random/arbitrary/`, but have my +1. – ch3ka Nov 21 '14 at 16:22
  • The only guarantee (which is useful) is that if you call `keys` and `values` without modifying the dict in between, the value in a given position will correspond with the key in the same position. – hobbs Nov 21 '14 at 16:25
  • ok, so if I were to add another key to the dictionary, the order of existing elements would be preserved and the key would simply be inserted into some implementation dependent location? – user3684792 Nov 21 '14 at 16:41
  • @user3684792, Based on the linked answer in the duplicate question, yes. – merlin2011 Nov 22 '14 at 17:29