Dictionary is an unordered, mutable, dynamic size collection datatype in Python. Dictionary stores elements in key-value pairs, where elements are indexed by keys. Dictionary keys can only be of immutable datatype but values can be of any datatype. From Python version 3.7 and later, dictionaries maintain insertion order.
In previous articles, we learned about various methods and operations that we can perform on Dictionary in Python. In this article we will work on practice programs related to Python Dictionary.
Program – Problem Statement
We will be given dictionary with students scores list. This dictionary key
will be student_id
and value
will be the list of scores for that student. We need to calculate the final grade for all the students based on their average scores. Grade will be calculated based on the given criteria:
- A – for average score >= 90%
- B – for average score >= 80%
- C – for average score >= 70%
- D – for average score >= 60%
- E – for average score < 60%
Output should be a dictionary with student_id as key and calculated grade as value.
Test Case 1 Input: input_dict = {"stu1": [76, 87, 65, 77], "stu2": [87, 57, 69, 74]} Output: {'stu1': 'C', 'stu2': 'C'} Test Case Explanation Dictionaryinput_dict
with student id and list of student scores is given as an input. This dictionary has student ids as keystu1
andstu2
and their value is a list of numbers, which represent the scores of a student. For each student we will calculate the average score for that student. i.e forstu1
average score will be76.25 = (76 + 87 + 65 + 77)/4
and forstu
2 average score will be71.75 = (87 + 57 + 69 + 74)/4
. For the average score of a student, we will calculate the grade baed on given grade criteria. i.e.stu1 = 76.25
is greater than 70 but less than 80, so grade will beC
and forstu2 = 71.75
is greater than 70 but less than 80, so grade will beC
. Output the dictionary with key as student id and value as grade for that student like{'stu1': 'C', 'stu2': 'C'}
.
Program – Solution
Steps for Program solution
- Dictionary
input_dict
with key-value pairs will be given as an input. Key as a student id and value as list of student scores. - Declare an empty dictionary
output_dict
for output. - Using
for
loop, iterate through all the elements ofinput_dict
and get the nextkey
from the dictionary elements. - Get the
value
asstu_list
for thekey
from the dictionary. This value will be a list of numbers. - Define a variable
total_score = 0
. This will be used to calculate total sum of scores for a student. - Using
for
loop, iterate through thestu_list
and calculate sum of all the numbers into variabletotal_score
. - calculate the average score
average_score
usingtotal_score
divided by total length of list i.elen(stu_list)
. - Based on
if, elif
andelse
conditions, calculate the grade criteria for theaverage_score
. - Add the key value pair into the output dictionary with key as student id and value as grade for the student.
Program – Test Cases
Test Case 1
Input: input_dict = {"stu1": [76, 87, 65, 77], "stu2": [87, 57, 69, 74]}
Output: {'stu1': 'C', 'stu2': 'C'}
Test Case 2
Input: input_dict = {"stu1": [76, 87, 85, 77], "stu2": [87, 67, 89, 74], "stu3": [87, 77, 89, 94]}
Output: {'stu1': 'B', 'stu2': 'C', 'stu3': 'B'}
Test Case 3
Input: input_dict = {"stu1": [93, 87, 85, 97], "stu2": [82, 97, 83, 74], "stu3": [77, 74, 88, 93]}
Output: {'stu1': 'A', 'stu2': 'B', 'stu3': 'B'}
Test Case 4
Input: input_dict = {"stu1": [73, 57, 55, 47], "stu2": [52, 77, 63, 54], "stu3": [87, 44, 58, 63]}
Output: {'stu1': 'E', 'stu2': 'D', 'stu3': 'D'}
Program – Solution Code
input_dict = {"stu1": [76, 87, 85, 77], "stu2": [87, 67, 89, 74], "stu3": [87, 77, 89, 94]}
output_dict = {}
for key in input_dict:
stu_list = input_dict[key]
total_score = 0
for m in stu_list:
total_score += m
average_score = total_score / len(stu_list)
if average_score >= 90:
output_dict[key] = "A"
elif average_score >= 80:
output_dict[key] = "B"
elif average_score >= 70:
output_dict[key] = "C"
elif average_score >= 60:
output_dict[key] = "D"
else:
output_dict[key] = "E"
print(output_dict)
Program 1 Output – {'stu1': 'B', 'stu2': 'C', 'stu3': 'B'}
Summary
We analysed and wrote a complete Python program including Dictionary datatype. Following scenario were covered:
Code – Github Repository
All code snippets and programs for this article and for Python tutorial, can be accessed from Github repository – Comments and Docstring in Python.