简体   繁体   中英

python map value to value instead of two key-value pairs

I currently have this right now

[{'label': 'ID', 'value': '8'}, {'label': 'Document', 'value': 'Authority Visit'}]

I wanted it to be

[{'ID':'8'},{'Document':'Authority Visit'}]

Thanks for helping.

You can just iterate over your list and then access both label and values per item:

sample_dict = [
  {'label': 'ID', 'value': '8'},
  {'label': 'Document', 'value': 'Authority Visit'}
]

result = [{item['label']:item['value']} for item in sample_dict]

print(result)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM