Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!ecngs!feeder.ecngs.de!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'string.': 0.04; 'value,': 0.04; 'type,': 0.07; '>>>>': 0.09; 'immutable': 0.09; 'interacting': 0.09; 'loop.': 0.09; 'dictionaries': 0.16; 'warn': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'int': 0.18; 'insert': 0.19; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'earlier': 0.23; 'dictionary': 0.23; 'cc:2**0': 0.26; 'module': 0.26; 'value.': 0.28; "i'm": 0.28; 'gis': 0.29; 'mapping': 0.29; 'matches': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'seem': 0.29; 'pm,': 0.29; 'key,': 0.30; 'values': 0.32; 'thanks': 0.32; 'list': 0.32; 'header:User- Agent:1': 0.33; 'object': 0.33; 'file': 0.34; 'easiest': 0.34; 'skip:i 40': 0.34; 'moving': 0.35; 'two': 0.36; 'help!': 0.37; 'entry': 0.37; 'another': 0.37; 'received:192': 0.38; 'could': 0.38; 'some': 0.38; 'doing': 0.38; 'data': 0.38; 'files': 0.39; 'put': 0.40; 'might': 0.40; 'your': 0.61; 'header:Reply-To:1': 0.70; 'anything.': 0.71; 'soon': 0.72; 'reply-to:no real name:2**0': 0.72; 'constraint': 0.84; 'grabbing': 0.84; 'something.': 0.84; 'unique.': 0.84; 'data)': 0.91 Date: Mon, 06 Feb 2012 23:43:36 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 Thunderbird/3.1.16 MIME-Version: 1.0 To: noydb Subject: Re: building a dictionary dynamically References: <4ea33a86-a329-45e1-a765-0cdacd8de57f@m24g2000yqb.googlegroups.com> In-Reply-To: <4ea33a86-a329-45e1-a765-0cdacd8de57f@m24g2000yqb.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:80EE2H+DicIGyoEdMx4tNZD5x8iLvYWZZy7ujR4dfOL PI0P/1PgQ/YESix7lfMe0F5bLNxEqRGgWcyaZQHqiZJC7XBuRd tsPNnS/aqXaj2ryKUEK3lRlUqbfrXspKzbezxgF3QHEQMV97wu MCcI7LGgaadPJYKnafNOf6Zxv71J545Hkfz9t3m1XEEF7fAnG8 cxxHJQaOXzssZ6tsrpVA8bI17HXnpqD76dF/0IlNlrYW5JKT0J OkkFTHaQCT6ZhhxSyoywuPKZKnUg2l/aYeh3uyx2goIPGiJjwY UJJpmpbV4kWsBfqVxjnn8l/cprNE9VKL+mZh111PmYggl4hHkH Hqd9U5/xpzLebO5/DeH4= Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328589827 news.xs4all.nl 6972 [2001:888:2000:d::a6]:40665 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19934 On 02/04/2012 01:13 PM, noydb wrote: > How do you build a dictionary dynamically? Doesn't seem to be an > insert object or anything. So I need an empty dictionary that I then > want to populate with values I get from looping through a list and > grabbing some properties. So simply, I have (fyi, arcpy = module for > interacting with gis data) > >>>> inDict = {} >>>> for inFC in inFClist: >>>> print inFC >>>> inCount = int(arcpy.GetCount_management(inFC).getOutput(0)) > > where I want to make a dictionary like {inFC: inCount, inFC: > inCount, ....} > > How do I build this??? > > And, is dictionaries the best route go about doing a comparison, such > that in the end I will have two dictionaries, one for IN and one for > OUT, as in I'm moving data files and want to verify that the count in > each file matches between IN and OUT. > > Thanks for any help! A dictionary is a mapping from key to value. So each entry consists of a key and a value, where the key is unique. As soon as you add another item with the same key, it overwrites the earlier one. The only other important constraint is that the key has to be of an immutable type, such as int or string. So you want to add the current inFC:inCount as an item in the dictionary? Put the following in your loop. inDict[inFC] = inCount You might also want to check if the key is a duplicate, so you could warn your user, or something. Easiest way to do that is if inFC in inDict: -- DaveA