appending key-value pairs to a dict

Roy Smith roy at panix.com
Fri May 20 16:50:39 EDT 2005


rbt  <rbt at athop1.ath.vt.edu> wrote:
>I know how to setup an empty list and loop thru something... appending 
>to the list on each loop... how does this work with dicts?
>
>I'm looping thru a list of files and I want to put the file's name and 
>its sha hash into a dict on each loop.

You just assign values to keys.  If the key doesn't exist, it's
created automagically.  You want something like this:

shaDict = {}
for fileName in fileNameList:
    hash = generateShaHash (fileName)
    shaDict[hash] = fileName



More information about the Python-list mailing list