Exercises with Solutions for the Python Dictionary
The purpose of this Python dictionary exercise is to teach and practice dictionary operations for Python developers. Python 3 is used to test each question.
The data are presented as key-value pairs in the mutable Python dictionary object. A colon separates each key’s value from each key. ( : ).
The most used data structure is the dictionary, so it is important to comprehend how it works. The following are included in this Python dictionary exercise:
–
- It contains 10 dictionary questions and solutions provided for each question.
- Practice different dictionary assignments, programs, and challenges.
It addresses queries regarding the following
topics:
- Dictionary operations and manipulations
- Dictionary functions
- Dictionary comprehension
Each question’s solution helps you become better acquainted with the Python dictionary. If you have any other suggestions, please let us know. It’ll assist others.
developers.
-
Use
Online Code Editor
to solve exercise questions
. -
Read the
complete guide to
Python dictionaries
to solve this exercise
a table
contents
-
Exercise 1: Convert two lists into a dictionary
-
Exercise 2: Merge two Python dictionaries into one
-
Exercise 3: Print the value of key ‘history’ from the below dict
-
Exercise 4: Initialize dictionary with default values
-
Exercise 5: Create a dictionary by extracting the keys from a given dictionary
-
Exercise 6: Delete a list of keys from a dictionary
-
Exercise 7: Check if a value exists in a dictionary
-
Exercise 8: Rename key of a dictionary
-
Exercise 9: Get the key of a minimum value from the following dictionary
-
Exercise 10: Change value of a key in a nested dictionary
Exercise 1: Create a dictionary from two lists.
The two lists are listed below. Create a Python program to turn them into a dictionary where the first item from each list serves as the key and the second item serves as the definition.
value
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
Run Predicted result:
display@@@0 Show Hint
Utilize the zip() function. This function aggregates two or more iterables (such as a list, dict, or string) into a tuple.
, and gives it back.
Or, use a for loop to iterate the list.
, range()
function. Use the update command to add a new key-value pair to a dict each time.() method Demonstrate the solution
1: A dict and the zip() function()
function Object() { [native code] }
-
Use the
zip(keys, values)
to aggregate two lists. -
Wrap the result of a
zip()
function into a
dict()
constructor.
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
# empty dictionary
res_dict = dict()
for i in range(len(keys)):
res_dict.update({keys[i]: values[i]})
print(res_dict)
Run
Alternative 2
: Making use of the loop and update() methods of a
dictionary
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
dict3 = {**dict1, **dict2}
print(dict3)
Run
Combine two Python dictionaries in exercise two.
one
sampleDict = {
"class": {
"student": {
"name": "Mike",
"marks": {
"physics": 70,
"history": 80
}
}
}
}
Run Predicted result:
{'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
Python version 3.5+ Show Solution
employees = ['Kelly', 'Emma']
defaults = {"designation": 'Developer', "salary": 8000}
Run
Various Versions
sample_dict = {
"name": "Kelly",
"age": 25,
"salary": 8000,
"city": "New york"}
# Keys to extract
keys = ["name", "salary"]
Run
Print the value of the key “history” from the following in Exercise 3.
dict
sample_dict = {
"name": "Kelly",
"age": 25,
"salary": 8000,
"city": "New york"}
# keys to extract
keys = ["name", "salary"]
# new dict
res = dict()
for k in keys:
# add current key with its va;ue from sample_dict
res.update({k: sample_dict[k]})
print(res)
Run Predicted result:
80 Show Indicator
The dict is nested. Find the required key-value pair by using the proper key chaining. Display Solution
sample_dict = {
"name": "Kelly",
"age": 25,
"salary": 8000,
"city": "New york"
}
# Keys to remove
keys = ["name", "salary"]
sample_dict = {k: sample_dict[k] for k in sample_dict.keys() - keys}
print(sample_dict)
Run
Exercise 4: Set default values for the dictionary’s initialization
The keys can be initialized in Python with the same values. Given
:
sample_dict = {
"name": "Kelly",
"age":25,
"salary": 8000,
"city": "New york"
}
Run Predicted result:
@@@2 output Show Hint
Use the dict’s fromkeys() method. Display Solution
A dictionary with the supplied keys and the specified values is returned by the fromkeys() method.
value.
sample_dict = {
'Physics': 82,
'Math': 65,
'history': 75
}
Run
Exercise 5: Build a dictionary by removing the keys from one that is already there.
construct a Python program to take the keys from the dictionary below and extract them to construct a new dictionary. provided diction
:
sample_dict = {
'emp1': {'name': 'Jhon', 'salary': 7500},
'emp2': {'name': 'Emma', 'salary': 8000},
'emp3': {'name': 'Brad', 'salary': 500}
}
Run Predicted result:
display@@@3 Show Hint
Solution 1:
- Iterate the mentioned keys using a loop
- Next, check if the current key is present in the dictionary, if it is present, add it to the new dictionary
Show Solution
Word reference
Comprehension
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
# empty dictionary
res_dict = dict()
for i in range(len(keys)):
res_dict.update({keys[i]: values[i]})
print(res_dict)
2
Run
Alternative 2
: Making use of the update() method
loop
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
# empty dictionary
res_dict = dict()
for i in range(len(keys)):
res_dict.update({keys[i]: values[i]})
print(res_dict)
4
Run
Remove a list of keys from a dictionary in exercise six
:
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
# empty dictionary
res_dict = dict()
for i in range(len(keys)):
res_dict.update({keys[i]: values[i]})
print(res_dict)
6
Run Predicted result:
display@@@4 Show Hint
- Iterate the mentioned keys using a loop
- Next, check if the current key is present in the dictionary, if it is present, remove it from the dictionary
We can use a dictionary’s pop() method or dictionary comprehension to get the aforementioned result. Solution 1 for Display
Description Employing the pop() technique and a loop
sample_dict equals Kelly, 25, makes $8,000 per year and lives in New York.
}
# Removed keys
[“Name,” “Salary”] in keys
keys: sample_dict.pop for k(k)
print(sample_dict) Alternate Solution
Word reference
Comprehension
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
# empty dictionary
res_dict = dict()
for i in range(len(keys)):
res_dict.update({keys[i]: values[i]})
print(res_dict)
8
Run
Exercise 7: Verify whether a value is present in a dictionary.
We are aware of how to determine if a dictionary has the key. There are situations when it is necessary to verify that the given value is present.
To determine whether the value 200 is included in the following dictionary, write a Python program. Given :
sample_dict is “a”: 100, “b”: 200, and “c”: 300.” expected result
@@@5 output Show Hint
-
Get all values of a dict in a list using the
values()
method. - Next, use the if condition to check if 200 is present in the given list
Show Answer
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
0
Run
Exercise 8: Rename the dictionary’s key
Create a program to change the name of a significant city to one from the following dictionaries. Given
:
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
2
Run Predicted result:
display@@@6 Show Hint
- Remove the city from a given dictionary
- Add a new key (location) into a dictionary with the same value
Show Answer
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
4
Run
Exercise 9: From the following, obtain the key with the lowest value.
dictionary
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
6
Run Predicted result:
Math Show Suggestions
Utilize the native function min() Show Solution
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
8
Run
Change the value of a key in a nested dictionary in exercise 10
To alter Brad’s pay to $8500 in the following dictionary, create a Python application. Given
:
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
dict3 = {**dict1, **dict2}
print(dict3)
0
Run Predicted result:
@@@7 output: Show Solution
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
dict3 = {**dict1, **dict2}
print(dict3)
2
Run