472,142 Members | 1,273 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

appending key-value pairs to a dict

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

Many thanks,

rbt
Jul 19 '05 #1
5 1818
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.


Whereas with a list you would call "append" in the loop, with a
dictionary you simply use an indexed-assignment type of access:

mydict = {}
for filename in some_list_of_filenames:
hash = sha.sha(open(filename).read()).hexdigest() # or whatever
mydict[filename] = hash

-Peter
Jul 19 '05 #2
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
Jul 19 '05 #3
On Friday 20 May 2005 01:04 pm, 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.

Many thanks,

rbt


Simple assignment.

adict[filename] = an_sha_hash

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #4
rbt <rb*@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
Jul 19 '05 #5
rbt
Peter Hansen wrote:
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.

Whereas with a list you would call "append" in the loop, with a
dictionary you simply use an indexed-assignment type of access:

mydict = {}
for filename in some_list_of_filenames:
hash = sha.sha(open(filename).read()).hexdigest() # or whatever
mydict[filename] = hash

-Peter


Thanks guys... that works great.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by kylei | last post: by
13 posts views Thread by Shwetabh | last post: by
2 posts views Thread by pyscottishguy | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.