0

I'm trying to search through multiple nested dictionaries like this one

dict1 = {'x':'y1'}
dict2 = {'a':{'x':'y2'}}  
dict3 = {'a':'b','c':{'d':{'x':'y3'}}}
...

So the problem is when I try to look for 'x', the location of 'x' in each dictionary changes. Is there a simple way to find 'x' in every dictionary like .findall('.//tag') when it comes to finding a tag through XML?

J.Shim
  • 17
  • 1
  • What if the key `'x'` occurs multiple times? – timgeb Jul 03 '18 at 09:38
  • @timgeb Keys cannot appear multiple times in the same dict. – rst-2cv Jul 03 '18 at 09:38
  • Do you want to find the value associated with the key `'x'` or the "path" to that key? – jdehesa Jul 03 '18 at 09:39
  • 2
    @ResetACK Keys can appear multiple times at different levels of the nested dictionary. – jdehesa Jul 03 '18 at 09:40
  • Ah true - head isn't on straight – rst-2cv Jul 03 '18 at 09:40
  • @jdehesa finding the value is enough but ideally the path associated to that key as well. – J.Shim Jul 03 '18 at 09:48
  • 1
    @J.Shim For the value the linked duplicate should work, for the path there are a few related question you can look up (e.g. [this](https://stackoverflow.com/q/33940483/1782792), [this](https://stackoverflow.com/q/45230554/1782792) or [this](https://stackoverflow.com/q/11929904/1782792)). If you can find a solution to your exact problem you can edit this question or ask a new one indicating your specific issue (and maybe with an [MCVE](https://stackoverflow.com/help/mcve) with your attempt). – jdehesa Jul 03 '18 at 09:53
  • @jdehesa thanks for the help. Yeah, I should've made clear that I want to know if there exists a single python command that would do the work(at least to find values) for me. – J.Shim Jul 03 '18 at 10:04

0 Answers0