appending key-value pairs to a dict

R

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
 
P

Peter Hansen

rbt said:
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
 
B

Brian Beck

rbt said:
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])
 
J

James Stroud

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/
 
R

Roy Smith

rbt said:
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
 
R

rbt

Peter said:
rbt said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top