fork download
  1. def func(target, dict):
  2. if not dict:
  3. return []
  4. for key , value in dict.items():
  5. if key == target:
  6. return [key] #return list with single element as key
  7. returnValue = func(target , value)
  8. if returnValue:
  9. return [key] + returnValue ; #append key to returnValue
  10. return []
  11.  
  12. inpDict = {
  13. '1' : {'2':{}},
  14. '3' : {'4':{},'5':{}},
  15. '6': {'7':{'8':{'9':{},'10':{}}}},
  16. '11':{}
  17. }
  18. print(func('5',inpDict))
Success #stdin #stdout 0.04s 9648KB
stdin
Standard input is empty
stdout
['3', '5']