0% found this document useful (0 votes)
17 views3 pages

Abdul-Azeez Adeyinka UoPeople Computer Science Assignment 122

This document describes a Python program that reads a dictionary from a file, inverts the key-value pairs, and writes the inverted dictionary to another file. It opens an input file, loads the dictionary, creates an empty inverted dictionary, loops through the original dictionary to invert the key-value pairs, and writes the result to an output file.

Uploaded by

adeyinka azeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Abdul-Azeez Adeyinka UoPeople Computer Science Assignment 122

This document describes a Python program that reads a dictionary from a file, inverts the key-value pairs, and writes the inverted dictionary to another file. It opens an input file, loads the dictionary, creates an empty inverted dictionary, loops through the original dictionary to invert the key-value pairs, and writes the result to an output file.

Uploaded by

adeyinka azeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

This is a Python program that reads a dictionary from a file, inverts the key-value pairs, and

writes the inverted dictionary to another file. The program uses the following steps:

1. Open the input file in read mode and use the eval function to convert the file content into a
Python dictionary object. Assign the dictionary to a variable called original_dict.

2. Create an empty dictionary called inverted_dict to store the inverted key-value pairs.

3. Loop through each key-value pair in original_dict and check if the value is a list or a single
item. If it is a list, loop through each item in the list and add it as a key to inverted_dict with the
original key as its value. If it is a single item, add it as a key to inverted_dict with the original
key as its value.

4. Open the output file in write mode and use the str function to convert the inverted_dict into a
string. Write the string to the output file and close both files.

The code and its output are shown below:

# Input file content

apple: red

banana: yellow

cherry: red

mango: yellow

grapes: black, green

}
# Python code

# Open the input file and read the dictionary

input_file = open("input.txt", "r")

original_dict = eval(input_file.read())

input_file.close()

# Create an empty dictionary for the inverted key-value pairs

inverted_dict = {}

# Loop through each key-value pair in original_dict

for key, value in original_dict.items():

# Check if the value is a list or a single item

if isinstance(value, list):

# Loop through each item in the list

for item in value:

# Add the item as a key and the original key as its value to inverted_dict

# If the item already exists as a key, append the original key to its value list

if item in inverted_dict:

inverted_dict[item].append(key)

else:

inverted_dict[item] = [key]

else:

# Add the value as a key and the original key as its value to inverted_dict
# If the value already exists as a key, append the original key to its value list

if value in inverted_dict:

inverted_dict[value].append(key)

else:

inverted_dict[value] = [key]

# Open the output file and write the inverted dictionary

output_file = open("output.txt", "w")

output_file.write(str(inverted_dict))

output_file.close()

# Output file content

'red': ['apple', 'cherry'],

'yellow': ['banana', 'mango'],

'black': ['grapes'],

'green': ['grapes']

You might also like