dictionary - Key Order in Python Dictionaries -
code:
d = {'a': 0, 'b': 1, 'c': 2} l = d.keys() print l this prints ['a', 'c', 'b']. i'm unsure of how method keys determines order of keywords within l. however, i'd able retrive keywords in "proper" order. proper order of course create list ['a', 'b', 'c'].
you use ordereddict (requires python 2.7) or higher.
also, note ordereddict({'a': 1, 'b':2, 'c':3}) won't work since dict create {...} has forgotten order of elements. instead, want use ordereddict([('a', 1), ('b', 2), ('c', 3)]).
as mentioned in documentation, versions lower python 2.7, can use this recipe.
Comments
Post a Comment