This is a more complicated version of the problem but it is still essentially creating a dictionary with a for loop. I wanted to create a database of all my photos/videos on my computer - 1000s of them and create a dictionary record for each photo which used the name of the photo/movie to identify the record. That way I would have unique dictionary records from which to create the database
Step to achieve this.
1) Using another StackOverflow answer but modifying it slightly I got the list of files and their Modified date, (which on Windows better reflected the date they were taken, rather than created date, which was the date the files was created on the Windows machine):
from stat import S_ISREG, ST_MTIME, ST_MODE
import os, sys, time
destdir="C:/Pictures/jiphone/jiPhone-2017"
# get all entries in the directory w/ stats
entries = (os.path.join(destdir, fn) for fn in os.listdir(destdir))
entries = ((os.stat(path), path) for path in entries)
# leave only regular files, insert creation date
entries = ((stat[ST_MTIME], path)
for stat, path in entries if S_ISREG(stat[ST_MODE]))
2) Then I created the dictionary
dict_J_iphone_2017=dict()
3) Then in a For loop over the entries I made recFile (the file record) from the name of the file, removing the fullstop (but you could have made it from whatever you were looping over)
for cdate, path in sorted(entries):
fullDate=time.ctime(cdate)
basePath=os.path.basename(path)
recFile=basePath.replace(".","")
etc
4) I then created the dictionary entry for each photo/movie
dict_J_iphone_2017[recFile]={'name': recName, 'date': recDate, 'time':recTime, 'day': recDay,'tags': [], 'folder': recFolder, 'comment': recComment, 'rating': 0}
and I am able to reference each item in the dictionary by putting in the file's actual name e.g.
print(dict_J_iphone_2017['JiP6_1291PNG']
#{'date': '5-Mar-2017', 'folder': 'C:/Pictures/jiphone/jiPhone-2017', 'tags': [], 'comment': 'PNG', 'name': 'JiP6_1291.PNG', 'time': '16:19:19', 'rating': 0, 'day': 'Sun '}
Hope that helps.