Creating a dictionary with a path name and SHA-256 hash The purpose is just to create a dictionary with a path name, along with the file's SHA-256 hash.

I'm very new to Python, and have a feeling that there's a much better way to implement the following code. It works, I'd just like to clean it up a bit.
    fnamelst = [r'C:\file1.txt',
                r'C:\file2.txt']

[fname.replace('\\', '\\\\') for fname in fnamelst]

diction = [{fname: hashlib.sha256(open(fname, 'rb').read()).digest()} for fname in fnamelst]

for iter in range(0,len(fnamelst)):
    dic2 = {fnamelst[iter]: hashlib.sha256(open(fnamelst[iter], 'rb').read()).digest()}
In your code, you show two ways to create your dictionary, but both are somewhat flawed:


diction will be a list of dictionaries, each holding just one entry
dic2 will get overwritten in each iteration of the loop, and will finally hold only one dictionary, again with only one element.


Instead, try this:

diction = dict([ (fname, hashlib.sha256(open(fname, 'rb').read()).digest())
                 for fname in fnamelst ])


Or shorter, for Python 2.7 or later:

diction = { fname: hashlib.sha256(open(fname, 'rb').read()).digest()
                 for fname in fnamelst }


The first one uses a list comprehension to create a list of key-value tuples, and then uses the dict function to create a single dictionary from that list. The second one does the same, using a dict comprehension.