Re: appending key-value pairs to a dict



rbt 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.

Like so:

d = {}
for filename in files:
    d[sha_func(filename)] = filename


Or like so:

d = dict([(sha_func(filename), filename) for filename in files])

--
Brian Beck
Adventurer of the First Order
.



Relevant Pages

  • Re: appending key-value pairs to a dict
    ... 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. ... Prev by Date: ...
    (comp.lang.python)
  • Re: appending key-value pairs to a dict
    ... 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. ...
    (comp.lang.python)
  • Re: appending key-value pairs to a dict
    ... 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. ...
    (comp.lang.python)
  • Re: appending key-value pairs to a dict
    ... how does this work with dicts? ... >its sha hash into a dict on each loop. ... You just assign values to keys. ... for fileName in fileNameList: ...
    (comp.lang.python)
  • appending key-value pairs to a dict
    ... 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. ...
    (comp.lang.python)