Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.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; 'operator': 0.03; 'represents': 0.05; 'tree': 0.05; 'nested': 0.07; 'wednesday,': 0.07; 'exist,': 0.09; 'thus,': 0.09; 'python': 0.11; 'stored': 0.12; "'b',": 0.16; "['a',": 0.16; 'dict': 0.16; 'keyerror': 0.16; 'path.': 0.16; 'roy': 0.16; 'subclass': 0.16; 'subject:item': 0.16; 'thursday,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'import': 0.22; 'alternate': 0.24; '(or': 0.24; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; '>>>>': 0.31; 'keys': 0.31; 'received:google.com': 0.35; 'there': 0.35; 'raising': 0.36; 'sequence': 0.36; 'shorter': 0.36; 'version:': 0.36; 'subject:?': 0.36; 'mapping': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'range': 0.61; "you'll": 0.62; 'more': 0.64; 'different': 0.65; 'smith': 0.68; '2.7.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=LQd1ZSeMJ/VgsYZLNo7Da5gMpk0zr6y7xL6Q9/cy8qc=; b=b42qXnqjrWSg0A+OYyOSRejacv6aydUUxJSqCj0JOQGPx0RQEAfGVmu7Mckbk+aobj KLmPhKv9jADGHzDrFc0qoGhKOC0cvTGTpVopaO0P4aLWe/BxOnPcL+GY3JQr6XxtP3xp /ay61m5iDko/3prK3WbNDx7y8JhWLEs4YnAKXkhPxxYdZ8q+kIoPVol+zuSXu4FPP704 EHkbfFlb9LBvnJf8Kgznp7tt1GGWNVxPY8BtGQ3zUHLPgXvNhV0pgzhQLierCw/KRa+h S8uZJxwOUp1OCkv9tVfBprXkgfdj8CfFvJpMWZ1nZo53Vojf079GaPWshGxicGp4MHMG FUSA== X-Received: by 10.68.110.165 with SMTP id ib5mr5276995pbb.61.1396508450890; Thu, 03 Apr 2014 00:00:50 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <141eca25-744b-47c0-8b2b-d8d08f8b27ef@googlegroups.com> References: <5d184a3a-c8d2-40d6-8055-7f7d1eb96127@googlegroups.com> <141eca25-744b-47c0-8b2b-d8d08f8b27ef@googlegroups.com> From: Ian Kelly Date: Thu, 3 Apr 2014 01:00:10 -0600 Subject: Re: Retrieve item deep in dict tree? To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396508455 news.xs4all.nl 2837 [2001:888:2000:d::a6]:58922 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69571 On Wed, Apr 2, 2014 at 10:15 PM, Rustom Mody wrote: > On Thursday, April 3, 2014 8:11:33 AM UTC+5:30, Rustom Mody wrote: >> On Wednesday, April 2, 2014 11:28:16 PM UTC+5:30, 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. Diffe= rent 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. > >> What you are asking for is probably: > >> >>> reduce((lambda tr, att: tr[att]), ['a','b','c'], nested) >> 'Hiii!!' > > Shorter version: > >>>> reduce(dict.get, ['a','b','c'], nested) > 'Hiii!!' That breaks if the dicts are ever replaced with an alternate mapping implementation (or a dict subclass that overrides the get method), and incorrectly returns None instead of raising KeyError if the last key in the sequence does not exist (and if any of the other keys don't exist, you'll get a TypeError instead of a KeyError). This is more robust: import operator reduce(operator.getitem, ['a', 'b', 'c'], nested)