If I have some json data represented in a dictionary type, what’s the pythonic way of finding all ‘type’ nodes that match a particular value from the ‘results’ list of items?
"results" : [
{
"address_components" : [
... child elements ...
],
"verified" : "some data here",
"geometry" : {
... child elements ...
},
"types" : [ "type_one" ]
},
{
"address_components" : [
... child elements ...
],
"verified" : "some data here",
"geometry" : {
... child elements ...
},
"types" : [ "type_two" ] // filter all items that match this item's value
},
... more elements ...
]
You can use a list comprehension:
Of course you’ll probably use a variable instead of hard-coding
'type_two'like that.This will allow other values in the
typeslist alongside the one you are looking for. You can useif elem['types'] == ['type_two']instead to look for that specific value only.