0% found this document useful (0 votes)
3 views5 pages

Dictionary Inversion in Python

The document discusses the process of inverting a dictionary in Python, where students are keys and their enrolled courses are values, transforming it so that courses become keys with lists of students as values. It details the implementation of a function that uses nested loops to efficiently restructure the data, ensuring multiple students in the same course are grouped together. This assignment emphasizes the significance of loops and dictionary operations in Python for data manipulation in various applications.

Uploaded by

samie3068536
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)
3 views5 pages

Dictionary Inversion in Python

The document discusses the process of inverting a dictionary in Python, where students are keys and their enrolled courses are values, transforming it so that courses become keys with lists of students as values. It details the implementation of a function that uses nested loops to efficiently restructure the data, ensuring multiple students in the same course are grouped together. This assignment emphasizes the significance of loops and dictionary operations in Python for data manipulation in various applications.

Uploaded by

samie3068536
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

Dictionary Inversion in Python

Introduction

In Python programming, dictionaries are powerful data structures that store data in key-
value pairs. In this assignment, we are required to manipulate a dictionary containing
students and their enrolled courses, and invert it so that courses become keys while
students become values. This type of transformation is useful in real world applications
such as academic systems where teachers may want to quickly identify which students
are enrolled in a specific course.

Input:
Output:
Technical Explanation

The program defines a function called invert dictionary() that takes a dictionary where
keys represent students and values are lists of courses. The goal is to reverse this
structure so that each course becomes a key, and the associated value is a list of
students enrolled in that course.

First, an empty dictionary called inverted_dict is created to store the results. The
program then uses a for loop with the items() method to iterate through each key-value
pair in the original dictionary. Here, student represents the key and courses represents
the list of courses.

Inside this loop, another loop iterates through each course in the list. For each course,
the program checks whether it already exists as a key in inverted dict. If it does not
exist, a new key is created with the course name and the student is added as the first
element in a list. If the key already exists, the student’s name is appended to the
existing list.

This approach ensures that multiple students enrolled in the same course are grouped
together under one key. The use of nested loops allows traversal of both dictionary
elements and list items efficiently.

The output confirms that the dictionary has been correctly inverted. Each course now
maps to a list of students, satisfying the teacher’s requirement. This demonstrates how
iteration and dictionary manipulation can be combined to restructure complex data
effectively.

Conclusion

This assignment highlights the importance of understanding loops and dictionary


operations in Python. By using iteration and conditional logic, we successfully
transformed the structure of the data. Such techniques are widely applicable in data
processing, database management, and real-world systems where relationships
between entities must be reorganized.

References
Downey, A. B. (2015). Think Python: How to think like a computer scientist (2nd ed.).
Green Tea Press.
[Link]

kjdElectronics. (2017, August 5). Python beginner tutorial 8 - For loop, lists, and
dictionaries [Video]. YouTube. [Link]

Raj, A. (2022, March 3). Reverse a dictionary in Python. PythonForBeginners.


[Link]

You might also like