Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.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.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'cc:addr:python-list': 0.11; "'size'": 0.16; 'assignments': 0.16; 'behaviour.': 0.16; 'collections': 0.16; 'defaultdict': 0.16; 'dictionary,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'md5': 0.16; 'pythonic': 0.16; 'subclass': 0.16; 'size,': 0.16; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'sets': 0.30; 'message- id:@mail.gmail.com': 0.30; 'asked': 0.31; 'lines': 0.31; 'sep': 0.31; 'file': 0.32; 'regular': 0.32; 'url:python': 0.33; 'sense': 0.34; 'something': 0.35; 'received:google.com': 0.35; 'subject:data': 0.36; 'next': 0.36; 'method': 0.36; 'url:org': 0.36; 'level': 0.37; 'easiest': 0.38; 'url:library': 0.38; 'does': 0.39; 'structure': 0.39; 'new': 0.61; 'url:3': 0.61; "you're": 0.61; 'you.': 0.62; 'here:': 0.62; 'places': 0.64; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=//7EBPvjPkfmOtx2JeCpa7Ghhfqy9qpDTcRFQ6tq9sI=; b=iHae4Z8z8/ySnPdpPed9lQnahbYhhHn0y+mtrBZ2+jX05GF0iUFzxIdAFed7MZHfdy gyZWyN/4m+HM798u53tCU8O2t1znh2fYa9eLlhxTZBALhpW8oFUKImtN0cSYQYFjd3lH tWlI9r6HV7l9BIGHmX2pdL50f+OTGp1/rVPWh3dqHBdH5Ts3RxYc8aO2IJQzpVE1mnki xup+O5kHGbtKRYcMAQnTKWeGtgJAih058lMGi2y2ay90gi6ENHfwfbsMh0TYxQ7fAXxn BF3VYObvK8B+EQbb+VwQE+ycWENVRkhTviu9/re0AnP7c1AEqxfNz2puVX5PZEMvq6SF bogw== MIME-Version: 1.0 X-Received: by 10.152.23.69 with SMTP id k5mr36192582laf.70.1411950020784; Sun, 28 Sep 2014 17:20:20 -0700 (PDT) In-Reply-To: References: Date: Mon, 29 Sep 2014 10:20:20 +1000 Subject: Re: trouble building data structure From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411950022 news.xs4all.nl 2849 [2001:888:2000:d::a6]:36860 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78445 On Mon, Sep 29, 2014 at 10:04 AM, David Alban wrote: > file_data = {} > > [... as i loop through lines in the file ...] > > file_data[ md5sum ][ inode ] = { 'path' : path, 'size' : size, } > > what is the pythonic way to build my "file_data" data structure above that > has the above structure? The easiest way would be with a defaultdict. It's a subclass of dictionary that does what you're looking for. You'd use it something like this: from collections import defaultdict file_data = defaultdict(dict) # then continue with the program as normal # including the loop and assignments that you have above Any time it's asked to look up an MD5 that doesn't exist yet, it'll create a new dictionary by calling dict(), and that sets up the next level for you. Docs are here: https://docs.python.org/2/library/collections.html#collections.defaultdict https://docs.python.org/3/library/collections.html#collections.defaultdict You can also use the setdefault() method of the regular dictionary, which makes sense if you have just a few places where you need this behaviour. ChrisA