I'm sure this is very easy:
If I have the following:
mydict = { 'key1': 'value1', 'key2': 'value2', ... , 'keyn': 'valuen' }
How can I end up w/ a list such as:
result = [ { 'key1': 'value1'}, { 'key2': 'value2' }, ...., 'keyn': 'valuen' }]
result = [{k: v} for (k, v) in mydict.iteritems()]
I think the easiest way to do this is list comprehension
mydict = { 'key1': 'value1', 'key2': 'value2', ... , 'keyn': 'valuen' }
new_list = [{k: v} for k, v in mydict.iteritems()]
or not so interesting version
map(lambda x, y: {x: y}, mydict.iteritems())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.