Reputation: 9850
I have a list of specifications that have a dict of properties associated with them:
specs = {
'spec': {'name': 'color', 'value': 'blue'},
'spec': {'name':'size', 'value':'8'}
}
Ultimately, I'd like to extract only size = 8 from this list, but the order of where size
is in the dict changes (ie. it's not always the second element).
Is there a more efficient way to find the dict that size
is in, other than looping through each dict item in specs?
I know that I could do specs [size] if it were located as a key.. but it's not.
Upvotes: 0
Views: 123
Reputation: 4392
You won't get any more efficient than looping through the list. You can write a nice, compact list comprehension if you want to:
print [x['spec']['value'] for x in specs if x['spec']['name'] == 'size']
# [8]
Upvotes: 2