0

I currently my OrderedDict like below.

d = [OrderedDict([('id', u'570c737e87f8dc15788790b7'), ('name', u'Murgh ')]), OrderedDict([('id', u'570c737e87f8dc15788790b6'), ('name', u'Mushroom')])]

But here i need to convert this to normal dictionary format like below.

{"id": "570c737e87f8dc15788790b7","name": "Murgh"
  },

{
"id": "570c737e87f8dc15788790b6","name": "Mushroom"
  }

How to do this from python.

user1335606
  • 479
  • 2
  • 5
  • 14
  • This question does not really seem to be a duplicate of http://stackoverflow.com/questions/20166749/how-to-convert-an-ordereddict-into-a-regular-dict-in-python3, since the data structure in the question is not an OrderedDict, but a list of OrderedDicts, and the desired output also seems to be different. – fmarc May 31 '16 at 13:15

1 Answers1

0

You can initialize a new dict with data from each of your OrderedDicts like this:

dicts = [dict(_) for _ in d]

Alternatively, to get a dict with ids as keys and names as values:

users = { od['id']:od['name'] for od in d }
fmarc
  • 1,706
  • 15
  • 20
  • 1
    @LutzHorn: Correct. I hope the updated answer reflects that – fmarc May 31 '16 at 12:49
  • @LutzHorn thank.But still its display as list dict i want only dict. – user1335606 May 31 '16 at 12:54
  • @user1335606: Ah, I get it. A dictionary can contain each key only once. So you can't have the key 'id' multiple times. The desired output you gave in your question also is a list/tuple of dicts. Could you update your question to show how the dict should look? – fmarc May 31 '16 at 12:56
  • actually i am writing web services above result i got from the serializers.py. so in Postman is display as a List dict which is causing a problem. – user1335606 May 31 '16 at 13:21