building a dictionary dynamically

Richard Thomas chardster at gmail.com
Sat Feb 4 21:18:30 EST 2012


On Feb 4, 6:13 pm, noydb <jenn.du... at gmail.com> 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!

Dictionaries are mutable, you can modify them in place:

>>> myDict = {}
>>> for myKey in myList:
...     myDict[myKey] = doSomething(myKey)

Dictionaries sound like a good way to go. You only need the one
dictionary though, the IN one. When processing the outCount values you
can just check them against the inDict at that point.

Regards,
Chard.



More information about the Python-list mailing list