I would like to transform a dict of one format into a dict of another format.
The raw_input
dict will have the shape:
'key': ['list of strings']
I would like to reformat it into a list of dicts with the following shape:
[
{'key': 'string'},
{'key': 'string'},
...etc for each item in the list of strings
]
My implementation is functional but naive, using a doubly nested for loop:
raw_input = {
'error': ['string 1', 'string2'],
'error2': ['string 3']
}
def return_dict_as_list(raw_input):
mylist = []
for foo in raw_input:
for bar in raw_input[foo]:
mylist.append( { 'mykey': foo, 'myvalue': bar } )
return mylist
The output is as expected but I can't help but think there's a better way.
I did try a couple of list comprehensions, but the output was not what was desired:
for example this:
elist = [[{'field': item, 'message': msg} for msg in raw[item]] for item in raw]
returns nested lists within a list, which I could unpack but doesn't seem very zen.
here's an online repl with the code: https://repl.it/repls/UnpleasantBiodegradableSystems