You are on page 1of 3

Learning journal unit 8

# Original program from Learning Journal Unit 7


my_dict = {
'apple': 3,
'banana': 2,
'orange': 4,
'pear': 1,
'kiwi': 5,
'peach': 6
}

# Inverting the dictionary


inv_dict = {}
for key, value in my_dict.items():
inv_dict.setdefault(value, []).append(key)

# Printing the inverted dictionary


for key, value in inv_dict.items():
print(key, ':', value)
In this program, we have a dictionary called my_dict that we are inverting using a for loop
and creating a new dictionary called inv_dict. The resulting inv_dict contains the original
dictionary's values as keys, and the original dictionary's keys as values.
To modify this program to read from a file, we can start by creating a text file that contains
our original dictionary's items. We can use the following format, where each line represents a
key-value pair separated by a colon:
makefile
apple:3
banana:2
orange:4
pear:1
kiwi:5
peach:6
We can save this file as original_dict.txt. Then, we can modify our program to read from this
file and create a dictionary from its contents.
# Reading from file and creating original dictionary
with open('original_dict.txt', 'r') as file:
original_lines = file.readlines()
my_dict = {}
for line in original_lines:
key, value = line.strip().split(':')
my_dict[key] = int(value)
# Inverting the dictionary
inv_dict = {}
for key, value in my_dict.items():
inv_dict.setdefault(value, []).append(key)
# Writing the inverted dictionary to a file
with open('inverted_dict.txt', 'w') as file:
for key, value in inv_dict.items():
file.write(str(key) + ':' + ','.join(value) + '\n')
In this modified program, we use a with statement to open the original_dict.txt file in read
mode and read its contents using the readlines() method. We then loop through each line, split
it into a key and value using the split() method, and create a dictionary called my_dict from
these key-value pairs.

We then invert this dictionary in the same way as before, creating a new dictionary called
inv_dict. Finally, we open a new file called inverted_dict.txt in write mode using another with
statement, loop through the items in inv_dict, and write each key-value pair to the file in the
same format as our original input file.

The output file inverted_dict.txt would look like this:


makefile
3:apple,kiwi
2:banana
4:orange
1:pear
5:kiwi
6:peach
In summary, we have modified our original program from Learning Journal Unit 7 to read the
dictionary items from a file, invert the dictionary, and write the inverted dictionary to a
different file. We used the open() function to read from and write to files, and the strip() and
split() methods to process the lines in the input file. We also used the setdefault() method to
create the inverted dictionary, and the join() method to format the output file.
Here are the references for the sources I used in my response:
For information on reading and writing files in Python, I used the official Python
documentation: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

To learn more about the setdefault() method used in the program, I referred to this Stack
Overflow post: https://stackoverflow.com/questions/3483520/use-cases-for-the-setdefault-
dict-method

For information on the split() method used to process lines in the input file, I used the Python
documentation: https://docs.python.org/3/library/stdtypes.html#str.split

To learn more about the join() method used to format the output file, I referred to this article
on Real Python: https://realpython.com/python-string-split-concatenate-join/

You might also like