Q:

How to make two text files into a dictionary(small fix)?

For example,  say I have two text files, test_keys.txt
 2
 5
7

and test_values.txt
ace
ventura
pet detective

My code to make these files into a dictionary is as follows.
with open('test_keys.txt') as file1:
    keys = file1.readlines()
with open('test_values.txt') as file2:
    values = file2.readlines()
print(sorted(dict(zip(keys,values)).items()))

To which I get
[(' 2\n', 'ace\n'), (' 5\n', 'ventura\n'), ('7', 'pet detective')]

How can I get rid of the \n and the space before the numbers in my result? I tried .replace('\n', '') after file1.readlines() and I get a, 'list' object has no attribute 'replace', error. Any suggestions?

A:

Use list comprehension to convert keys to numbers and strip the values:
keys = [int(key) for key in keys]
values = [value.strip() for value in values]

Then follow your code.

