On Feb 5, 4:13 am, noydb <jenn.du...@gmail.com> wrote: > How do you build a dictionary dynamically? > >>> 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???
The easiest way is to use the standard dictionary constructor. dict() can accept a list of key/value pairs: >>> pairs = [('a',1), ('b',2), ('c',3)] >>> dict(pairs) {'a': 1, 'c': 3, 'b': 2} And one way to build a list is with a list comprehension: >>> pairs = [(key, i+1) for i, key in enumerate(['a','b','c'])] >>> pairs [('a', 1), ('b', 2), ('c', 3)] So you can combine the two, using a list comprehension to build the list that is passed into dict(): >>> dict([(key, i+1) for i, key in enumerate(['a','b','c'])]) {'a': 1, 'c': 3, 'b': 2} As a convenience, you don't need the square brackets: >>> dict((key, i+1) for i, key in enumerate(['a','b','c'])) {'a': 1, 'c': 3, 'b': 2} For your example, I'd go with something like this: getCount = lambda x: int(arcpy.GetCount_management(x).getOutput(0)) inDict = dict( (inFC, getCount(inFC)) for inFC in inFClist ) Hope this helps. -- http://mail.python.org/mailman/listinfo/python-list