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

CS 1101 Learning Journal W7

week 7 learning journal

Uploaded by

brittisaacs19
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)
10 views3 pages

CS 1101 Learning Journal W7

week 7 learning journal

Uploaded by

brittisaacs19
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

The inver_dictionary function creates the inverted dictionary using a dictionary comprehension

and nested loops to iterate through the original dictionary's values and courses. It creates key-

value pairs in the inverted dictionary for each course, with the value being a list comprehension

that identifies students associated with the course. As a result, courses are represented as keys,

and the students enrolled in each course are represented as values. The program then prints both

the original and inverted dictionaries, demonstrating the effectiveness of dictionary

comprehension in producing the desired result in a readable and concise manner.


# Creating Inverted dictionary function

This section defines a function named inverting_dictionary that takes one argument, origin_dict,

which is the original dictionary to be inverted.

#Inverted Dictionary Comprehension

This line creates the inverted dictionary (inver_dict) using dictionary comprehension. It iterates

through each course in each list of courses in the values of the original dictionary. It generates a

key-value pair in the inverted dictionary for each course. The course is the key, and the value is a

list comprehension that searches the original dictionary for students associated with that course.

Downey (2015) (pp. 104, 106)

# Orginal Dictionary:

This section defines the original dictionary, with students as keys and lists of courses as values

#Printing Original Dictionary:

It prints a header indicating that the following output is the original dictionary, and then prints

the original dictionary.


#Calling and Printing Inverted Dictionary:

It calls the inverting_dictionary function with the original dictionary and stores the result in

inver_dict. It prints a header indicating that the following output is the inverted dictionary, and

then prints the inverted dictionary.

References: Author, A. D. (2015). Think Python

How to Think Like a Computer Scientist (2nd ed.) Allen Downey.

https://greenteapress.com/thinkpython2/thinkpython2.pdf

You might also like