Mastering Python Dictionaries: Unleashing the Power of Advanced Key-Value Magic

·

3 min read

Table of contents

In our preceding blog post, we delved into the fundamental aspects of Python Dictionaries, examining their structure and basic functionalities, including operations such as value modification and the addition of new entries. The focus of this subsequent article will be on exploring advanced applications of Python Dictionaries.

Example 1

In this part, you're creating a dictionary named student_grades. Each key is a student's name ("Alice," "Bob," "Charlie"), and the corresponding value is another dictionary representing the grades for that student in different subjects.

  • This section calculates the average grade for each student.

  • It initializes an empty dictionary named average_grades to store the calculated averages.

  • It then uses a for loop to iterate through the items (student names and their grades) in the student_grades dictionary.

  • For each student, it calculates the total score by summing up the grades using sum(grades.values()).

  • It calculates the average by dividing the total score by the number of grades using len(grades).

  • The average grade is then assigned to the average_grades dictionary with the student's name as the key.

  • This part finds the student with the highest average grade using the max function.

  • The key parameter specifies that the maximum should be determined based on the values in the average_grades dictionary.

  • The max function returns the key (student's name) with the highest average grade.

Finally, this line prints a message indicating the student with the highest average grade along with their average grade. It uses an f-string to format the output. Therefore, we get the below result:

Example 2

In this part, we create a dictionary named capital_cities_and_countries where each key is a capital city, and the corresponding value is the associated country.

This line uses the input() function to take user input. The user is prompted to enter the name of a country, and the entered text is stored in the variable user_input

  • This part checks if the user-input country is in the values of the capital_cities_and_countries dictionary.

  • If the country is found, it uses a list comprehension [key for key, value in capital_cities_and_countries.items() if value == user_input] to find the corresponding key (capital city) for the entered country. The [0] at the end retrieves the first (and only) item in the list.

  • It then prints a message indicating the capital of the entered country.

  • If the country is not found, it prints a message indicating no country with the entered capital.

Therefore, we get the below result when typing the country "USA".

Type another country not in the capital_cities_and_countries dictionary and you will get the below result.

Thank you for your time today. I apologize for any discrepancies in the output. Regrettably, my Visual Studio encountered operational issues when executing the Example 2 code.

Wishing you all a Merry Christmas. Stay tuned, as the next article may delve into lists.