I also think it is important to note that Python dict
object type is a hash table (more on this here), and thus is not capable of being sorted without converting its keys/values to lists. What this allows is dict
item retrieval in constant time O(1)
, no matter the size/number of elements in a dictionary.
Having said that, once you sort its keys - sorted(data.keys())
, or values - sorted(data.values())
, you can then use that list to access keys/values in design patterns such as these:
for sortedKey in sorted(dictionary):
print dictionary[sortedKeY] # gives the values sorted by key
for sortedValue in sorted(dictionary.values()):
print sortedValue # gives the values sorted by value
Hope this helps.