In python I have a dict as below :
{'name':{0:'tom',1:'dav'}, 'age':{0:22,1:23}}
I want change it to this:
[{'name':'tom','age':22}, {'name':'dav','age':23}]
Can you answer with the simplest way of doing this?
How about something like this?
>>> d = {'name':{0:'tom',1:'dav'}, 'age':{0:22,1:23}}
>>> values = zip(*[value.values() for value in d.values()])
>>> l = [{'name': name, 'age': age} for name, age in values]
>>> l
[{'name': 'tom', 'age': 22}, {'name': 'dav', 'age': 23}]
[value.values() for value in d.values()] return [['tom', 'dav'], [22, 23]] which's the values of your subdicts, and then, zip() return [('tom', 22), ('dav', 23)].
Then, we use for loop over the zipped values, and put the values into the dicts in l.