0

What is the principle of sorting is used in Python?

>>> dict = {'a':'paul', 'b':'max', 'c':'bill'}
>>> dict
{'a': 'paul', 'c': 'bill', 'b': 'max'}

Why not?

{'a': 'paul', 'b': 'max', 'c': 'bill'}
MFerguson
  • 1,739
  • 9
  • 17
  • 30
Max Khrichtchatyi
  • 507
  • 1
  • 7
  • 14
  • because dictionaries are not sorted. theoretically, the output may be in any order. in your case, it's supposedly in mid-order – gefei Sep 26 '13 at 10:43

1 Answers1

3

Dictionaries in python are unordered collections:

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).

Though, you can have an ordered dictionary by using collections.OrderedDict.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 4
    Note that "ordered" is distinct from "sorted" though. OrderedDict maintains insertion order, it doesn't sort anything. –  Sep 26 '13 at 10:46