Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exist,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; "'size'": 0.16; 'callable': 0.16; 'defaultdict': 0.16; 'dictionary.': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'size,': 0.16; 'wrote:': 0.18; "python's": 0.19; "skip:' 30": 0.19; '(in': 0.22; 'header :User-Agent:1': 0.23; 'received:comcast.net': 0.24; 'file.': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'needed.': 0.30; "i'm": 0.30; 'lines': 0.31; 'keyerror:': 0.31; 'perl': 0.31; 'file': 0.32; '(most': 0.33; "i'd": 0.34; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'add': 0.35; 'version': 0.36; 'subject:data': 0.36; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'new': 0.61; 'do:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: trouble building data structure Date: Sun, 28 Sep 2014 20:21:30 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411950104 news.xs4all.nl 2875 [2001:888:2000:d::a6]:38487 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78446 On 9/28/14 8:04 PM, David Alban wrote: > i'm writing a program to scan a data file. from each line of the data > file i'd like to add something like below to a dictionary. my perl > background makes me want python to autovivify, but when i do: > > file_data = {} > > [... as i loop through lines in the file ...] > > file_data[ md5sum ][ inode ] = { 'path' : path, 'size' : size, } > > i get: > > Traceback (most recent call last): > File "foo.py", line 45, in > file_data[ md5sum ][ inode ] = { 'path' : path, 'size' : size, } > KeyError: '91b152ce64af8af91dfe275575a20489' > > what is the pythonic way to build my "file_data" data structure above > that has the above structure? > If you want file_data to be a dictionary of dictionaries, use a defaultdict: file_data = collections.defaultdict(dict) This is Python's version of autovivification. When you access a key that doesn't exist, the defaultdict will use the callable you gave it (in this case, dict) to create the new value as needed. -- Ned Batchelder, http://nedbatchelder.com