OrderedDict does not save order
from collections import OrderedDict
import pprint
menu = {"about" : "about", "login" : "login", 'signup': "signup"}
menu = OrderedDict(menu)
pprint.pprint(menu.items())
import sys
sys.exit()
Output:
[('about', 'about'), ('signup', 'signup'), ('login', 'login')]
Thus, the order is not preserved even when used OrderedDict
. I know that dictionaries don't keep the initial default order and all that stuff. But I want to know why it OrderedDict
doesn't work.
+3
source to share
2 answers
Please find the code snippet below
>>> from collections import OrderedDict
>>> listKeyVals = [(1,"One"),(2,"Two"),(3,"Three"),(4,"Four"),(5,"Five")]
>>> x = OrderedDict(listKeyVals)
>>> x
OrderedDict([(1, 'One'), (2, 'Two'), (3, 'Three'), (4, 'Four'), (5, 'Five')])
>>>
I suggest you view examples from my article
https://techietweak.wordpress.com/2015/11/11/python-collections/
0
source to share