bob
0
Q:

python Ordered dict to dict

from collections import OrderedDict

# Remembers the order the keys are added!
x = OrderedDict(a=1, b=2, c=3)
1
An OrderedDict is a dictionary subclass that remembers the order 
that keys were first inserted. A regular dict doesn’t track the 
insertion order, and iterating it gives the values in an arbitrary 
order.
# A Python program to demonstrate working of OrderedDict 
from collections import OrderedDict 
d = {} # regular dict
d['a'] = 1
d['b'] = 2
d['c'] = 3
for key, value in d.items(): 
    print(key, value) 
# Output
# ('a', 1)
# ('c', 3)
# ('b', 2)  
od = OrderedDict() # Ordered Dict
od['a'] = 1
od['b'] = 2
od['c'] = 3
for key, value in od.items(): 
    print(key, value) 
# Output
# ('a', 1)
# ('b', 2)
# ('c', 3)
1
>>> from collections import OrderedDict
>>> dict(OrderedDict([('method', 'constant'), ('data', '1.225')]))
{'data': '1.225', 'method': 'constant'}
0

New to Communities?

Join the community