I want to create a dictionary in which the keys are taken from a list keys, and the values are lists of strings taken from several text files. Suppose the list keysand all files to read have the same number of lines.
How can I iterate over the list keysand lines of each file at the same time? My idea is to use zip(), but it did not work for me.
I know that I can iterate over lines in a file using:
currFile = open('myfile.txt', 'r')
for line in currFile:
And I know that I can iterate over two lists at the same time:
for foo, bar in zip(foos, bars):
But this does not work:
myDict = {}
keys = [17, 21, 8, 2, ..., 91]
currFile = open('myfile.txt', 'r')
for key, line in zip(keys, currFile):
myDict[key] = line
I could pull all the lines from the file to the list, pin it, and then run the loop, but that would not be very efficient.
How can I iterate over the list keysand lines in a file at the same time so that zip () is called dynamically?