I need to convert ordereddict to dictionary key value.
test = [OrderedDict([('key', 'value'),
('name', 'A'),
('value', '5')]),
OrderedDict([('key', 'value'),
('name', 'B'),
('value', '10')]),
OrderedDict([('key', 'value'),
('name', 'C'),
('value', '25')])]here
Desired Output:
{'A': '5', 'B': '10', 'C': '25'}
Your
test
is essentially a list of dictionaries. Each of those dictionaries has a key'name'
, which we want to be the key in our output, and a key'value'
, which we want to be the value in our output. So, we can just use a dict comprehension to iterate through the dicts contained bytest
, and transform each one into a single key-value pair.If you wanted the output to also be an
OrderedDict
, then you could feed a generator comprehension to the constructor: