Retrieve item deep in dict tree?
John Gordon
gordon at panix.com
Wed Apr 2 14:03:07 EDT 2014
In <mailman.8818.1396461529.18130.python-list at python.org> Roy Smith <roy at panix.com> writes:
> 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
> keys =3D ['foo', 'bar', 'baz']
> I want to retrieve tree['foo']['bar']['baz'].
> Is there some idiomatic, non-cryptic way to write that as a one-liner?
> I'm using Python 2.7.
How about three lines?
subtree = tree
for key in keys:
subtree = subtree.get(key)
--
John Gordon Imagine what it must be like for a real medical doctor to
gordon at panix.com watch 'House', or a real serial killer to watch 'Dexter'.
More information about the Python-list
mailing list