On Fri, 20 Jan 2023 at 17:30, Dino <d...@no.spam.ar> wrote: > > let's say I have this list of nested dicts: > > [ > { "some_key": {'a':1, 'b':2}}, > { "some_other_key": {'a':3, 'b':4}} > ] > > I need to turn this into: > > [ > { "value": "some_key", 'a':1, 'b':2}, > { "value": "some_other_key", 'a':3, 'b':4} > ]
You want both the key and the value so you can use items(): In [39]: L = [ ...: { "some_key": {'a':1, 'b':2}}, ...: { "some_other_key": {'a':3, 'b':4}} ...: ] In [40]: [{"value": k, **m} for l in L for k, m in l.items()] Out[40]: [{'value': 'some_key', 'a': 1, 'b': 2}, {'value': 'some_other_key', 'a': 3, 'b': 4}] -- Oscar -- https://mail.python.org/mailman/listinfo/python-list