Actually both the result you got in 'print(dicto)' and 'dicto' are same as your input but it is order by the alphabetical order of 'keys'.
If you want to preserve order of the keys as you have declared you can achieve it by the usage of 'OrdererdDict'. Here is the code.
Method1: Manual convertion
Code:
#import OrdererdDict module from collections
from collections import OrderedDict
dicto = [ OrderedDict({'type': 'convolutional', 'batch_normalize': '1', 'filters': '128', 'size': '3', 'stride': '2', 'pad': '1', 'activation': 'leaky'}), OrderedDict({'type': 'convolutional', 'batch_normalize': '1', 'filters': '64', 'size': '1', 'stride': '1', 'pad': '1', 'activation': 'leaky'})]
print(dicto)
Output:
[OrderedDict([('type', 'convolutional'), ('batch_normalize', '1'), ('filters', '128'), ('size', '3'), ('stride', '2'), ('pad', '1'), ('activation', 'leaky')]), OrderedDict([('type', 'convolutional'), ('batch_normalize', '1'), ('filters', '64'), ('size', '1'), ('stride', '1'), ('pad', '1'), ('activation', 'leaky')])]
You can get the same result without print() function also.
dicto
Output
[OrderedDict([('type', 'convolutional'),
('batch_normalize', '1'),
('filters', '128'),
('size', '3'),
('stride', '2'),
('pad', '1'),
('activation', 'leaky')]),
OrderedDict([('type', 'convolutional'),
('batch_normalize', '1'),
('filters', '64'),
('size', '1'),
('stride', '1'),
('pad', '1'),
('activation', 'leaky')])]
N.B: Don't worry about the OrderedDict comes in the display. It won;t affect the values. Here is the proof that you can access anything.
Accessing first dictionary from the list dicto:
dicto[0]
Output:
OrderedDict([('type', 'convolutional'),
('batch_normalize', '1'),
('filters', '128'),
('size', '3'),
('stride', '2'),
('pad', '1'),
('activation', 'leaky')])
Accessing value of key 'filters' of first dictionary from the list dicto expected value is '128':
dicto[0]['filters']
Output:
'128'
Method2: Automatic convertion
If you didn't understand and the first method looks so messy you can follow the second method. Here we iterate through you each dictionary elements in the list and makes it to Orderdict by using list comprehension method. Here is the code.
Code:
#import OrderedDict module from collections
from collections import OrderedDict
#your list dicto declared here
dicto = [ {'type': 'convolutional', 'batch_normalize': '1', 'filters': '128', 'size': '3', 'stride': '2', 'pad': '1', 'activation': 'leaky'}, {'type': 'convolutional', 'batch_normalize': '1', 'filters': '64', 'size': '1', 'stride': '1', 'pad': '1', 'activation': 'leaky'}]
#converting each dictionary element to oredered dictionaries by list comprehension
dicto = [OrderedDict(x) for x in dicto]
#display resulting list dicto
print(dicto)
Output:
[OrderedDict([('type', 'convolutional'), ('batch_normalize', '1'), ('filters', '128'), ('size', '3'), ('stride', '2'), ('pad', '1'), ('activation', 'leaky')]), OrderedDict([('type', 'convolutional'), ('batch_normalize', '1'), ('filters', '64'), ('size', '1'), ('stride', '1'), ('pad', '1'), ('activation', 'leaky')])]
I hope this would be helpful... :)