Find keys in multidimensional dict based on value
I have a dict of dicts that looks like the following:
{ 'a':{'foo': True,
'bar': 1,
'baz': 'abc'},开发者_运维技巧
'b':{'foo': True,
'bar': 21,
'baz': 'abc'},
'c':{'foo': True,
'bar': 3,
'baz': 'cba'}}
What is the most efficient way to filter the dict to only include those keys whose sub-values for 'baz' are 'abc'? So in the example above, it would return a dict that looked like:
{ 'a':{'foo': True,
'bar': 1,
'baz': 'abc'},
'b':{'foo': True,
'bar': 21,
'baz': 'abc'}}
{k: v for k, v in my_dict.items() if v['baz'] == 'abc'}
精彩评论