0

I have a list of OrderedDicts like this:

input = [OrderedDict([('id', 'mp-1'), ('name', 'John')]), OrderedDict([('id', 'mp-2'), ('name', 'Peter')])]

How can I convert it to a list of normal dicts like this

output = [{'id': 'mp-1', 'name': 'John'}, {'id': 'mp-2', 'name': 'Peter'}]
  • 3
    `list(map(dict,input))` – Ch3steR May 08 '20 at 15:25
  • The `dict` constructor can take a list of pairs as input so you can pass the `OrderedDict` to the `dict` constructor – rdas May 08 '20 at 15:27
  • Does this answer your question? [How to convert an OrderedDict into a regular dict in python3](https://stackoverflow.com/questions/20166749/how-to-convert-an-ordereddict-into-a-regular-dict-in-python3) – Ch3steR May 08 '20 at 15:27

2 Answers2

0

You can use dict constructor:

>>> output = [dict(i) for i in input]
>>> output
[{'id': 'mp-1', 'name': 'John'}, {'id': 'mp-2', 'name': 'Peter'}]
Vicrobot
  • 3,795
  • 1
  • 17
  • 31
0

Use this:

output = []
for odict in input:
    output.append(dict(odict))

Its not crazy fast, but its readable and good enough.

xilpex
  • 3,097
  • 2
  • 14
  • 45