Skip to content

Instantly share code, notes, and snippets.

@MishraKhushbu
Created February 21, 2018 05:32
Show Gist options
  • Save MishraKhushbu/7ff9cd853901809c67beb7d540a6a4c9 to your computer and use it in GitHub Desktop.
Save MishraKhushbu/7ff9cd853901809c67beb7d540a6a4c9 to your computer and use it in GitHub Desktop.
Python_Dictionary_21-Feb-2018
Since Python dictionary stores key value pairs, it uses hashing operations to store the values.
It is not necessary for the hash values to be in sorted order.
So when you print the dictionary the values are displayed on the basis of hash values and so python’s dictionary is un ordered.
If you want python dictionaries in sorted order, you can use sorted() function.
If want to use ordered dict in python we can use "ordereddict" module in short.
from _ordereddict import ordereddict
kio = ordereddict()
kvio = ordereddict(kvio=True)
# without relax unordered initalisation is not allowed
d = ordereddict({'a':1, 'b': 2}, relax=True)
sd = sorteddict({'a':1, 'b': 2}) # sorteddict is always relaxed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment