Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'represents': 0.05; 'tree': 0.05; 'nested': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'thus,': 0.09; '~ethan~': 0.09; 'python': 0.11; 'stored': 0.12; 'dict': 0.16; 'path.': 0.16; 'received:69.93': 0.16; 'roy': 0.16; 'subject:item': 0.16; 'top- level': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'header:User- Agent:1': 0.23; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "i'm": 0.30; '(which': 0.31; 'keys': 0.31; 'there': 0.35; 'sequence': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'structure': 0.39; 'to:addr:python.org': 0.39; 'range': 0.61; 'received:173': 0.61; 'different': 0.65; 'smith': 0.68; '2.7.': 0.84 Date: Wed, 02 Apr 2014 11:15:13 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Retrieve item deep in dict tree? References: <8E3FFB83-FDCA-4214-A8C6-8681200830DA@panix.com> In-Reply-To: <8E3FFB83-FDCA-4214-A8C6-8681200830DA@panix.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator3304.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source-IP: 173.12.184.233 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:40529 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3IzMzA0Lmhvc3RnYXRvci5jb20= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396463804 news.xs4all.nl 2970 [2001:888:2000:d::a6]:55158 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69552 On 04/02/2014 10:58 AM, Roy Smith wrote: > > 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 = ['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. Can you make the top-level tree a custom dict that understands tuple keys should be drilled down? -- ~Ethan~