<div dir="ltr"><div class="gmail_extra"><div class="gmail_extra">Here is a tricky method :</div><div class="gmail_extra"><br></div><div class="gmail_extra">reduce(lambda x,y:x[str(y)],keys,tree)</div><div class="gmail_extra">
<br></div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">---------- Forwarded message ----------</div><div class="gmail_extra">From: Roy Smith <<a href="mailto:roy@panix.com">roy@panix.com</a>></div>
<div class="gmail_extra">To: Python List <<a href="mailto:python-list@python.org">python-list@python.org</a>></div><div class="gmail_extra">Cc: </div><div class="gmail_extra">Date: Wed, 2 Apr 2014 13:58:16 -0400</div>
<div class="gmail_extra">Subject: Retrieve item deep in dict tree?</div><div class="gmail_extra">I have a big hairy data structure which is a tree of nested dicts.  I have a sequence of strings which represents a path through the tree.  Different leaves in the tree will be at different depths (which range from 1 to about 4 or 5 at most).  I want to get the value stored at that path.  Thus, if</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">keys = ['foo', 'bar', 'baz']</div><div class="gmail_extra"><br></div><div class="gmail_extra">I want to retrieve tree['foo']['bar']['baz'].</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">Is there some idiomatic, non-cryptic way to write that as a one-liner?</div><div class="gmail_extra"><br></div><div class="gmail_extra">I'm using Python 2.7.</div>
<div><br></div></div></div>