Lohith Arcot
Lohith Arcot

Reputation: 1186

Is there a way to select particular key value of dictionary present in a list of dictionaries

I am presently using a for loop to print all the required key value pairs of a dictionary. However is there a simpler way to select the required key value pair?

        for i in (out['elements']):
            out = (i['insights'][0]['details']['socialInfo'])
            out_temp.append(out)

The content of out is actually a JSON with list of dictionaries and each dictionary contains a list of dictionaries.

Upvotes: 0

Views: 208

Answers (2)

Du D.
Du D.

Reputation: 5310

You can use map to generate the new list as well. But I think what you are doing is fine, it is much easier to read than the alternatives.

out_temp = list(map(lambda x: x['insights'][0]['details']['socialInfo'], out['elements']))

Upvotes: 1

jpp
jpp

Reputation: 164783

I cannot see an unequivocally simpler way to access the data you require. However, you can apply your logic more efficiently via a list comprehension:

out_temp = [i['insights'][0]['details']['socialInfo'] for i in out['elements']]

Whether or not this is also simpler is open to debate.

Upvotes: 0

Related Questions