Introduction
In this tutorial, We will look as top 20 dictionary comprehension interview questions and solutions. Just like List or Set comprehension, Dictionary comprehension in Python is also a powerful tool for creating dictionaries in a concise and expressive manner, making Python code more readable and efficient.
Dictionary Comprehension Overview
Also read: How to Install Flask in VS Code and Build REST API : [ 9 Easy Steps]
Dictionary comprehension is a concise and readable way to create dictionaries in Python. It allows you to create dictionaries using a single line of code by specifying key-value pairs based on an iterable. The syntax of dictionary comprehension is similar to List comprehension. but it generates dictionaries instead of Lists.
Syntax of Dictionary Comprehension
{key_expression: value_expression for item in iterable}
NOTE:
Top 20 Dictionary Comprehension Interview Questions and Solutions (2023)
We will look at 20 problems in the next solution which are purely solved using dictionary comprehension technique. I will recommend to complete this tutorial till the end so that you get some hands on with the Dictionary data structure in Python.
Also read: Top 15 List Comprehension Interview Questions and Answers (2023)
Question-1: Create Dictionary of Square numbers
In this problem, you will be asked to create a dictionary which will contain square of numbers from 1 to 10 and print the dictionary. Below is the solution for this this problem.
square_dict = {x: x**2 for x in range(1, 11)} print(square_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
Question-2: Count Word Frequency in Dictionary
In this problem, you will be given a List of countries. Your job is to find the count of total number of times a word has appeared in the given List. Print the dictionary. Below is the solution for this problem.
Countries = ["India", "Pakistan", "China", "Japan", "India", "India", "America", "China", "India", "China"] count = {x: Countries.count(x) for x in Countries} print(count)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'India': 4, 'Pakistan': 1, 'China': 3, 'Japan': 1, 'America': 1}
Question-3: Create Dictionary from List
In this problem, you will be given a List of countries. Your job is to convert this List into dictionary where key will be contry name and value will be its length. Print the dictionary. below is the solution of this problem.
Countries = ["India", "Pakistan", "China"] count = {x: len(x) for x in Countries} print(count)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'India': 5, 'Pakistan': 8, 'China': 5}
Question-4: Create dictionary from Two Lists
In this problem, you will be given two Lists. First List will contain names and Second List will contain countries. Your job is to create a dictionary where key will be names from first List and values will be countries from second List. Print the dictionary. Below is the given solution
names = ["Ankush", "Marry", "Jacky"] countries = ["India", "Pakistan", "China"] new_dict = {x: y for x,y in zip(names,countries)} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'Ankush': 'India', 'Marry': 'Pakistan', 'Jacky': 'China'}
Question-5: Create Dictionary of Even numbers
In this problem, you will be asked to create a dictionary whose key will be number from 1 to 10 and values will be squares of keys respectively. Then filter out and keep only the even number values in the dictionary. Print the dictionary. Below is the solution.
even_dict = {x: x**2 for x in range(1, 11) if x % 2 ==0} print(even_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}
Question-6: Create dictionary of String-Vowel
In this problem, you will be given a List of countries. You have to create a dictionary whose key will be country name and value will be total vowels appeared in that country. Print the dictionary. Below is the solution.
countries = ["India", "Pakistan", "China", "America", "Africa"] new_dict = {x: sum(1 for i in x if i in 'aeiouAEIOU') for x in countries} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'India': 3, 'Pakistan': 3, 'China': 2, 'America': 4, 'Africa': 3}
Question-7: Create dictionary to Count Upper Case Letters
In this problem, you will be given a list of countries. Your job is to create a dictionary whose key will be country name and value will be the total number of upper case letter used in that country name. Print the dictionary. Below is the solution.
countries = ["IndiA", "PakISTan", "China", "AmeRIca", "AFRICA"] uppercase_count_dict = {country: sum(1 for char in country if char.isupper()) for country in countries} print(uppercase_count_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'IndiA': 2, 'PakISTan': 4, 'China': 1, 'AmeRIca': 3, 'AFRICA': 6}
Question-8: Create Dictionary of Numbers and Their Cubes
In this problem, you will be given a list of integers. Your job is to create a dictionary comprehension which will contain numbers divisible by 3 as key and their cubes as values. Print the dictionary. Below is the solution
num_list = [2, 3, 5, 7,9, 12, 13, 60, 63, 56, 55, 30] new_dict = {x: x**3 for x in num_list if x %3 == 0} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {3: 27, 9: 729, 12: 1728, 60: 216000, 63: 250047, 30: 27000}
Question-9: Create Dictionary of Country’s Temperature
In this problem, you will be given a dictionary of Country and it’s temperature as key-value pair. Your job is to convert the temperature into Fahrenheit and store in a new dictionary as value against the key as Country. Print the dictionary. Below is the solution.
countries = {"India" : 25 , "Pakistan" : 30, "China" : 28, "America" : 32, "Africa" : 40} new_dict = {country: (temp * 9/5) + 32 for country, temp in countries.items()} print(new_dict)
OUTPUT
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'India': 77.0, 'Pakistan': 86.0, 'China': 82.4, 'America': 89.6, 'Africa': 104.0}
Question-10: Create Dictionary of Words and its Length
In this problem, you will be given a List of Countries. Your job is to create a dictionary whose key will be words having length longer than 4 and its values will be total length of that word. Print the dictionary. Below is the solution.
countries = ["India", "Pakistan", "China", "America", "Africa", "Iraq", "Iran"] new_dict = {x: len(x) for x in countries if len(x) > 4} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'India': 5, 'Pakistan': 8, 'China': 5, 'America': 7, 'Africa': 6}
Question-11: Merge two Dictionary
In this problem, you will be given two Dictionaries. Your job is to merge these Dictionaries using Dictionary conprehension. Print the new Dictionary. Below is the solution.
dict1 = {"A" : 1, "Am" : 2, "I" : 3} dict2 = {"He" : 4, "She" : 5, "It" :6} new_dict = {key: value for dict in [dict1, dict2] for key, value in dict.items()} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'A': 1, 'Am': 2, 'I': 3, 'He': 4, 'She': 5, 'It': 6}
Question-12: Remove Elements from Dictionary
In this problem, you will be given a Dictionary. Your job is to create a new Dictionary which will only contain elements whose value is greater that 10. Print the new Dictionary. Below is the solution.
dict1 = {'A': 1, 'Am': 20, 'I': 3, 'He': 4, 'She': 15, 'It': 60, 'Any' : 44, 'Many' : 8} new_dict = {key: val for key, val in dict1.items() if val > 10} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'Am': 20, 'She': 15, 'It': 60, 'Any': 44}
Question-13: Create Dictionary of Factorial of Numbers
In this problem, you will be asked to create a dictionary whose key will be number from 1 to 5 and values will be factorial of those numbers.
factorial_dict = {} for n in range(6): if n == 0: factorial_dict[n] = 1 else: factorial_dict[n] = n * factorial_dict[n - 1] factorial_dict = {n: 1 if n == 0 else n * factorial_dict[n - 1] for n in range(6)} print(factorial_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {0: 1, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120}
Question-14: Create Dictionary using Word Slicing
In this problem, you will be given a List of Countries. Your job is to create a dictionary whose key will be first character of each word and value will be rest of the word character. Print the Dictionary. Below is the solution.
countries = ["India", "Pakistan", "China", "America", "Africa", "Iraq", "Iran"] new_dict = {char[0]: char[1:] for char in countries} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'I': 'ran', 'P': 'akistan', 'C': 'hina', 'A': 'frica'}
Question-15: Create Dictionary to Swap Key-Value
In this problem, you will be given a dictionary of Countries and its temperature as key value. Your job is to create a new dictionary where key value will be swapped for each element i.e temperature will become key and Country will become value. Print the new Dictionary. Below is the solution.
dict1 = {"India" : 25 , "Pakistan" : 30, "China" : 28, "America" : 32, "Africa" : 40} new_dict = {val: key for key, val in dict1.items()} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {25: 'India', 30: 'Pakistan', 28: 'China', 32: 'America', 40: 'Africa'}
Question-16: Create Dictionary of Minimum Values
In this problem, you will be given a dictionary of country and temperature. You job is to create a new dictionary which will only contain elements with minimum values. Print the new dictionary. Below is the solution.
dict1 = {"India" : 25 , "Pakistan" : 30, "China" : 28, "America" : 25, "Africa" : 40} min_val = min(dict1.values()) new_dict = {key: val for key, val in dict1.items() if val == min_val } print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'India': 25, 'America': 25}
Question-17: Create Dictionary of String and List
In this problem, you will be given a List of Countries. Your job is to create a new Dictionary which will contain length of each word as key and list of all words with same length as value. Print the new Dictionary. Below is the solution given.
dict1 = ["India", "Pakistan", "China", "America", "Africa", "Iraq", "Iran"] new_dict = {len(country): [x for x in dict1 if len(x) == len(country)] for country in dict1} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {5: ['China', 'India'], 8: ['Pakistan'], 4: ['Iran', 'Iraq'], 6: ['Africa'], 7: ['America']}
Question-18: Find Sum of Square of Even Numbers
In this problem, you will be asked to create a dictionary which will contain sum of squares of even numbers from 1 to 15 as key and List of those even numbers as value. Print the Dictionary. Below is the solution.
square_result = {sum(x **2 for x in range(1, 16) if x %2 == 0): [x for x in range(1, 16) if x % 2 == 0]} print(square_result)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {560: [2, 4, 6, 8, 10, 12, 14]}
Question-19: Convert List of Tuples to Dictionary
In this problem, you will be given a List of Tuples containing Countries and its temperature. Create a Dictionary where Countries will be keys and temperatures will be values. Print the Dictionary. Below is the solution.
country_list = [("India", 25) , ("Pakistan", 30), ("China", 28), ("America", 25), ("Africa", 40)] new_dict = {country: temperature for country, temperature in country_list} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'India': 25, 'Pakistan': 30, 'China': 28, 'America': 25, 'Africa': 40}
Question-20: Find Common Elements in List
In this problem you will be given two list of countries. Your Job is to find the common country names in both the Lists and create a new Dictionary which will contain common country name as key and total count of its appearance as value. Print the new Dictionary. Below is the solution.
country_list1 = ["Iran", "India", "India", "Iraq", "China", "Europe", "Pakistan"] country_list2 = ["Africa", "Iraq", "Iran", "India","Iraq", "India"] new_dict = {country: country_list1.count(country) + country_list2.count(country) for country in set(country_list1) & set(country_list2)} print(new_dict)
PS C:\Users\linuxnasa\OneDrive\Desktop\python_projects> python3 .\dict-comp.py {'Iran': 2, 'India': 4, 'Iraq': 3}
Summary
You can read more about Dictionary data structure and its features from python.org.