IUnknown IUnknown - 4 years ago 111
Python Question

extracting from list of dictionary

I have a list of dictionaries.
Keys in every dictionary in the list have another dictionary as a value.


I would need to list out the values of the 'source' key in all the child dictionaries in the list (one-liner if possible)- as a set.
Expected output:


Please assist.

Answer Source

If dlist is your list of dictionaries, then:

values = set([list(d.values())[0]['source'] for d in dlist])

Additionally, if you don't care about mutating the dictionaries, you can also do this:

values = set([d.popitem()[1]['source'] for d in dlist])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download