If you don't know at which depth the key will appear, you will need to march through the whole dictionary.
I was so free as to convert your data to an actual ordered dictionary. The function may yield more than one result in the case that the same key appears in different sub-directories:
from collections import OrderedDict
mydict = OrderedDict ( {'KYS_Q1AA_YouthSportsTrustSportParents_P':
OrderedDict ( {'KYS_Q1AA':
OrderedDict ( [ ('chart_layout', '3'),
('client_name', 'Sport Parents (Regrouped)'),
('sort_order', 'asending'),
('chart_type', 'pie'),
('powerpoint_color', 'blue'),
('crossbreak', 'Total')
] ) } ) } )
def listRecursive (d, key):
for k, v in d.items ():
if isinstance (v, OrderedDict):
for found in listRecursive (v, key):
yield found
if k == key:
yield v
for found in listRecursive (mydict, 'powerpoint_color'):
print (found)
If you are interested in where you have found the key, you can adapt the code accordingly:
def listRecursive (d, key, path = None):
if not path: path = []
for k, v in d.items ():
if isinstance (v, OrderedDict):
for path, found in listRecursive (v, key, path + [k] ):
yield path, found
if k == key:
yield path + [k], v
for path, found in listRecursive (mydict, 'powerpoint_color'):
print (path, found)