How do I get a given key's value from a list of dictionaries?
mylist = [{'powerpoint_color': 'blue','client_name': 'Sport Parents (Regrouped)'},{'sort_order': 'ascending','chart_layout': '1','chart_type': 'bar'}
]
The number of dictionaries in mylist
is unknown and I want to find the value attached to the key 'sort_order'
.
My failed attempt:
for key in mylist:for value in key:print(key['sort_order'])
mylist= [{'powerpoint_color': 'blue', 'client_name': 'Sport Parents (Regrouped)'}, {'sort_order': 'ascending', 'chart_layout': '1', 'chart_type': 'bar'}]print [d["sort_order"] for d in mylist if "sort_order" in d][0]
Result:
ascending
You could also combine all of the dictionaries into a single dict, and access that:
combined_d = {key: value for d in mylist for key,value in d.iteritems() }
print combined_d["sort_order"]