pprint sorts dicts keys alphabetically, print sorts them in default order.
from pprint import pprint
d = {'foo': 1, 'bar': 2, 'baz': 3}
pprint(d)
# {'bar': 2, 'baz': 3, 'foo': 1}
print d
# {'baz': 3, 'foo': 1, 'bar': 2}
The documentation of pprint mentions this, but does not say why. Why the discrepancy?
pprintstands for “pretty print”, also implying “pleasing to the human eye, and easily read by humans”. Sorting thedictkeys just follows that aim,pprintisn’t supposed to be primarily fast (sorting the keys adds a penalty), but, errr, pretty. 🙂printon the other hand “just prints”, as fast as possible. Actually the discrepancy here is betweendict‘s__str__andpprint‘s specially crafted string conversion.