Python-Ref > Data structures > Dictionaries > Order of items in dictionary
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Order of items in dictionary

As was said earlier, the order in which the items appear in the dictionary is completely random. It does neither reflect the order in which the data were added nor the value of keys.
The following example demonstrates this feature.
Expand/Shrink
Zdroj: (dict5-1.py)
  1   """default dictionary order"""
  2   
  3   name2age = {"Jane": 21, "Lucy": 34, "David": 1, "Tom": 5, "Peter": 6, "John": 56}
  4   
  5   print name2age  # no order here, completely random
  6   
  7   for key in name2age:
  8     print key, name2age[ key]  # the same order here - random
stdout:
{'Peter': 6, 'Lucy': 34, 'David': 1, 'Tom': 5, 'Jane': 21, 'John': 56}
Peter 6
Lucy 34
David 1
Tom 5
Jane 21
John 56
Doba běhu: 21.4 ms
It is often desirable to retrieve the values from a dictionary in some sorted order, such as alphabetically when the keys are names.
In this case we can simply retrieve the list of keys, sort it and then use this new sorted list for iteration.
Expand/Shrink
Zdroj: (dict5-2.py)
  1   """sorting by key"""
  2   
  3   name2age = {"Jane": 21, "Lucy": 34, "David": 1, "Tom": 5, "Peter": 6, "John": 56}
  4   
  5   print name2age  # no order here, completely random
  6   
  7   keys = name2age.keys()
  8   keys.sort()  # sort the keys
  9   for key in keys:  # iterate over the sorted keys
 10     print key, name2age[ key]
stdout:
{'Peter': 6, 'Lucy': 34, 'David': 1, 'Tom': 5, 'Jane': 21, 'John': 56}
David 1
Jane 21
John 56
Lucy 34
Peter 6
Tom 5
Doba běhu: 21.3 ms
In newer versions of Python (2.4 and above) a built-in function sorted can be used to accomplish the same result.
Expand/Shrink
Zdroj: (dict5-4.py)
  1   """sorting by key"""
  2   
  3   name2age = {"Jane": 21, "Lucy": 34, "David": 1, "Tom": 5, "Peter": 6, "John": 56}
  4   
  5   print name2age  # no order here, completely random
  6   
  7   for key in sorted( name2age): # sorted does sorting on the fly
  8     print key, name2age[ key]
  9   
 10   # or even
 11   print "---"
 12   for key,value in sorted( name2age.iteritems()): # it works with tuples too
 13     print key, value
stdout:
{'Peter': 6, 'Lucy': 34, 'David': 1, 'Tom': 5, 'Jane': 21, 'John': 56}
David 1
Jane 21
John 56
Lucy 34
Peter 6
Tom 5
---
David 1
Jane 21
John 56
Lucy 34
Peter 6
Tom 5
Doba běhu: 21.7 ms
In case the output should be sorted by value and not by key, we can use the approach described for lists in Advanced list sorting (this is also usable for sorting by key but there is a simple way available).
Expand/Shrink
Zdroj: (dict5-3.py)
  1   """sorting by value"""
  2   
  3   name2age = {"Jane": 21, "Lucy": 34, "David": 1, "Tom": 5, "Peter": 6, "John": 56}
  4   
  5   print name2age  # no order here, completely random
  6   
  7   pairs = [(v,k) for k,v in name2age.iteritems()]
  8   print pairs
  9   
 10   pairs.sort()
 11   for value, key in pairs:  # iterate over the sorted pairs
 12     print value, key
stdout:
{'Peter': 6, 'Lucy': 34, 'David': 1, 'Tom': 5, 'Jane': 21, 'John': 56}
[(6, 'Peter'), (34, 'Lucy'), (1, 'David'), (5, 'Tom'), (21, 'Jane'), (56, 'John')]
1 David
5 Tom
6 Peter
21 Jane
34 Lucy
56 John
Doba běhu: 21.1 ms