w3resource

Python Data Types: Dictionary - Exercises, Practice, Solution


This resource offers a total of 400 Python Dictionary problems for practice. It includes 80 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]

Python dictionary is a container of the unordered set of objects like lists. The objects are surrounded by curly braces { }. The items in a dictionary are a comma-separated list of key:value pairs where keys and values are Python data type.

You may read our Python dictionary tutorial before solving the following exercises.


1. Sort Dictionary by Value

Write a Python script to sort (ascending and descending) a dictionary by value.

Click me to see the sample solution


2. Add Key to Dictionary

Write a Python script to add a key to a dictionary.

Sample Dictionary : {0: 10, 1: 20}
Expected Result : {0: 10, 1: 20, 2: 30}

Click me to see the sample solution


3. Concatenate Dictionaries

Write a Python script to concatenate the following dictionaries to create a new one.

Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

Click me to see the sample solution


4. Check Key Existence in Dictionary

Write a Python script to check whether a given key already exists in a dictionary.

Click me to see the sample solution


5. Iterate Over Dictionary Using For Loops

Write a Python program to iterate over dictionaries using for loops.

Click me to see the sample solution


6. Generate Dictionary of Numbers and Their Squares

Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x).

Sample Dictionary ( n = 5) :
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Click me to see the sample solution


7. Dictionary with Keys 1 to 15 and Their Squares

Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both included) and the values are the square of the keys.

Sample Dictionary
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}
Click me to see the sample solution


8. Merge Two Python Dictionaries

Write a Python script to merge two Python dictionaries.

Click me to see the sample solution


9. Iterate Over Dictionaries (Alternative)

Write a Python program to iterate over dictionaries using for loops.

Click me to see the sample solution


10. Sum All Items in a Dictionary

Write a Python program to sum all the items in a dictionary.

Click me to see the sample solution


11. Multiply All Items in a Dictionary

Write a Python program to multiply all the items in a dictionary.

Click me to see the sample solution


12. Remove a Key from a Dictionary

Write a Python program to remove a key from a dictionary.

Click me to see the sample solution


13. Map Two Lists into a Dictionary

Write a Python program to map two lists into a dictionary.

Click me to see the sample solution


14. Sort Dictionary by Key

Write a Python program to sort a given dictionary by key.

Click me to see the sample solution


15. Get Maximum and Minimum Values of a Dictionary

Write a Python program to get the maximum and minimum values of a dictionary.

Click me to see the sample solution


16. Get Dictionary from an Object's Fields

Write a Python program to get a dictionary from an object's fields.

Click me to see the sample solution


17. Remove Duplicates from the Dictionary

Write a Python program to remove duplicates from the dictionary.

Click me to see the sample solution


18. Check if a Dictionary is Empty

Write a Python program to check if a dictionary is empty or not.

Click me to see the sample solution


19. Combine Two Dictionaries by Adding Values

Write a Python program to combine two dictionary by adding values for common keys.

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})
Click me to see the sample solution


20. Print All Distinct Values in a Dictionary

Write a Python program to print all distinct values in a dictionary.

Sample Data : [{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"}, {"V":"S009"},{"VIII":"S007"}]
Expected Output : Unique Values: {'S005', 'S002', 'S007', 'S001', 'S009'}
Click me to see the sample solution


21. Create Combinations of Letters from Dictionary Keys

Write a Python program to create and display all combinations of letters, selecting each letter from a different key in a dictionary.

Sample data : {'1':['a','b'], '2':['c','d']}
Expected Output:
ac
ad
bc
bd
Click me to see the sample solution


22. Find Highest 3 Values of Corresponding Keys in a Dictionary

Write a Python program to find the highest 3 values of corresponding keys in a dictionary.

Click me to see the sample solution


23. Combine Values in a List of Dictionaries

Write a Python program to combine values in a list of dictionaries.

Sample data: [{'item': 'item1', 'amount': 400}, {'item': 'item2', 'amount': 300}, {'item': 'item1', 'amount': 750}]
Expected Output: Counter({'item1': 1150, 'item2': 300})
Click me to see the sample solution


24. Create Dictionary from a String (Letter Frequency)

Write a Python program to create a dictionary from a string.

Note: Track the count of the letters from the string.
Sample string : 'w3resource'
Expected output: {'w': 1, '3': 1, 'r': 2, 'e': 2, 's': 1, 'o': 1, 'u': 1, 'c': 1}
Click me to see the sample solution


25. Print Dictionary in Table Format

Write a Python program to print a dictionary in table format.

Click me to see the sample solution


26. Count Values Associated with a Key in a Dictionary

Write a Python program to count the values associated with a key in a dictionary.

Expected Output:
6
2
Click me to see the sample solution


27. Convert List into Nested Dictionary of Keys

Write a Python program to convert a list into a nested dictionary of keys.

Click me to see the sample solution


28. Sort a List Alphabetically in a Dictionary

Write a Python program to sort a list alphabetically in a dictionary.

Click me to see the sample solution


29. Remove Spaces from Dictionary Keys

Write a Python program to remove spaces from dictionary keys.

Click me to see the sample solution


30. Get Top Three Items in a Shop

Write a Python program to get the top three items in a shop.

Sample data: {'item1': 45.50, 'item2':35, 'item3': 41.30, 'item4':55, 'item5': 24}
Expected Output:
item4 55
item1 45.5
item3 41.3
Click me to see the sample solution


31. Get Key, Value, and Item in a Dictionary

Write a Python program to get the key, value and item in a dictionary.

Click me to see the sample solution


32. Print Dictionary Line by Line

Write a Python program to print a dictionary line by line.

Click me to see the sample solution


33. Check if Multiple Keys Exist in a Dictionary

Write a Python program to check if multiple keys exist in a dictionary.

Click me to see the sample solution


34. Count Number of Items in Dictionary Value That is a List

Write a Python program to count the number of items in a dictionary value that is a list.

Click me to see the sample solution


35. Sort Counter by Value

Write a Python program to sort Counter by value.

Sample data : {'Math':81, 'Physics':83, 'Chemistry':87}
Expected data: [('Chemistry', 87), ('Physics', 83), ('Math', 81)]
Click me to see the sample solution


36. Create Dictionary from Two Lists Without Losing Duplicates

Write a Python program to create a dictionary from two lists without losing duplicate values.

Sample lists: ['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII'], [1, 2, 2, 3]
Expected Output: defaultdict(<class 'set'>, {'Class-V': {1}, 'Class-VI': {2}, 'Class-VII': {2}, 'Class-VIII': {3}})
Click me to see the sample solution


37. Replace Dictionary Values with Their Sums

Write a Python program to replace dictionary values with their sums.

Click me to see the sample solution


38. Match Key Values in Two Dictionaries

Write a Python program to match key values in two dictionaries.

Sample dictionary: {'key1': 1, 'key2': 3, 'key3': 2}, {'key1': 1, 'key2': 2}
Expected output: key1: 1 is present in both x and y
Click me to see the sample solution


39. Store Dictionary Data in a JSON File

Write a Python program to store dictionary data in a JSON file.

Original dictionary:
{'students': [{'firstName': 'Nikki', 'lastName': 'Roysden'}, {'firstName': 'Mervin', 'lastName': 'Friedland'}, {'firstName': 'Aron ', 'lastName': 'Wilkins'}], 'teachers': [{'firstName': 'Amberly', 'lastName': 'Calico'}, {'firstName': 'Regine', 'lastName': 'Agtarap'}]}
<class 'dict'>
Json file to dictionary:
{'students': [{'firstName': 'Nikki', 'lastName': 'Roysden'}, {'firstName': 'Mervin', 'lastName': 'Friedland'}, {'firstName': 'Aron ', 'lastName': 'Wilkins'}], 'teachers': [{'firstName': 'Amberly', 'lastName': 'Calico'}, {'firstName': 'Regine', 'lastName': 'Agtarap'}]}
Click me to see the sample solution


40. Create Dictionary with Keys 'x', 'y', 'z' and List Values

Write a Python program to create a dictionary of keys x, y, and z where each key has as value a list from 11-20, 21-30, and 31-40 respectively. Access the fifth value of each key from the dictionary.

{'x': [11, 12, 13, 14, 15, 16, 17, 18, 19],
'y': [21, 22, 23, 24, 25, 26, 27, 28, 29],
'z': [31, 32, 33, 34, 35, 36, 37, 38, 39]}
15
25
35
x has value [11, 12, 13, 14, 15, 16, 17, 18, 19]
y has value [21, 22, 23, 24, 25, 26, 27, 28, 29]
z has value [31, 32, 33, 34, 35, 36, 37, 38, 39]
Click me to see the sample solution


41. Drop Empty Items from Dictionary

Write a Python program to drop empty items from a given dictionary.

Original Dictionary:
{'c1': 'Red', 'c2': 'Green', 'c3': None}
New Dictionary after dropping empty items:
{'c1': 'Red', 'c2': 'Green'}
Click me to see the sample solution


42. Filter Dictionary Based on Values

Write a Python program to filter a dictionary based on values.

Original Dictionary:
{'Cierra Vega': 175, 'Alden Cantrell': 180, 'Kierra Gentry': 165, 'Pierre Cox': 190}
Marks greater than 170:
{'Cierra Vega': 175, 'Alden Cantrell': 180, 'Pierre Cox': 190}
Click me to see the sample solution


43. Convert Multiple Lists to a Nested Dictionary

Write a Python program to convert more than one list to a nested dictionary.

Original strings:
['S001', 'S002', 'S003', 'S004']
['Adina Park', 'Leyton Marsh', 'Duncan Boyle', 'Saim Richards']
[85, 98, 89, 92]
Nested dictionary:
[{'S001': {'Adina Park': 85}}, {'S002': {'Leyton Marsh': 98}}, {'S003': {'Duncan Boyle': 89}}, {'S004': {'Saim Richards': 92}}]
Click me to see the sample solution


44. Filter Students by Height and Weight

Write a Python program to filter the height and width of students, which are stored in a dictionary.

Original Dictionary:
{'Cierra Vega': (6.2, 70), 'Alden Cantrell': (5.9, 65), 'Kierra Gentry': (6.0, 68), 'Pierre Cox': (5.8, 66)}
Height > 6ft and Weight> 70kg:
{'Cierra Vega': (6.2, 70)}
Click me to see the sample solution


45. Verify All Dictionary Values are the Same

Write a Python program to verify that all values in a dictionary are the same.

Original Dictionary:
{'Cierra Vega': 12, 'Alden Cantrell': 12, 'Kierra Gentry': 12, 'Pierre Cox': 12}
Check all are 12 in the dictionary.
True
Check all are 10 in the dictionary.
False
Click me to see the sample solution


46. Group Key-Value Pairs into Dictionary of Lists

Write a Python program to create a dictionary grouping a sequence of key-value pairs into a dictionary of lists.

Original list:
[('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
Grouping a sequence of key-value pairs into a dictionary of lists:
{'yellow': [1, 3], 'blue': [2, 4], 'red': [1]}
Click me to see the sample solution


47. Split Dictionary of Lists into List of Dictionaries

Write a Python program to split a given dictionary of lists into lists of dictionaries.

Original dictionary of lists:
{'Science': [88, 89, 62, 95], 'Language': [77, 78, 84, 80]}
Split said dictionary of lists into list of dictionaries:
[{'Science': 88, 'Language': 77}, {'Science': 89, 'Language': 78}, {'Science': 62, 'Language': 84}, {'Science': 95, 'Language': 80}]
Click me to see the sample solution


48. Remove Specified Dictionary from a List

Write a Python program to remove a specified dictionary from a given list.

Original list of dictionary:
[{'id': '#FF0000', 'color': 'Red'}, {'id': '#800000', 'color': 'Maroon'}, {'id': '#FFFF00', 'color': 'Yellow'}, {'id': '#808000', 'color': 'Olive'}]
Remove id #FF0000 from the said list of dictionary:
[{'id': '#800000', 'color': 'Maroon'}, {'id': '#FFFF00', 'color': 'Yellow'}, {'id': '#808000', 'color': 'Olive'}]
Click me to see the sample solution


49. Convert String Values in Dictionary to Numeric Types

Write a Python program to convert string values of a given dictionary into integer/float datatypes.

Original list:
[{'x': '10', 'y': '20', 'z': '30'}, {'p': '40', 'q': '50', 'r': '60'}]
String values of a given dictionary, into integer types:
[{'x': 10, 'y': 20, 'z': 30}, {'p': 40, 'q': 50, 'r': 60}]
Original list:
[{'x': '10.12', 'y': '20.23', 'z': '30'}, {'p': '40.00', 'q': '50.19', 'r': '60.99'}]
String values of a given dictionary, into float types:
[{'x': 10.12, 'y': 20.23, 'z': 30.0}, {'p': 40.0, 'q': 50.19, 'r': 60.99}]
Click me to see the sample solution


50. Clear List Values in a Dictionary

A Python dictionary contains List as a value. Write a Python program to clear the list values in the said dictionary.

Original Dictionary:
{'C1': [10, 20, 30], 'C2': [20, 30, 40], 'C3': [12, 34]}
Clear the list values in the said dictionary:
{'C1': [], 'C2': [], 'C3': []}
Click me to see the sample solution


51. Update List Values in a Dictionary

A Python Dictionary contains List as a value. Write a Python program to update the list values in the said dictionary.

Original Dictionary:
{'Math': [88, 89, 90], 'Physics': [92, 94, 89], 'Chemistry': [90, 87, 93]}
Update the list values of the said dictionary:
{'Math': [89, 90, 91], 'Physics': [90, 92, 87], 'Chemistry': [90, 87, 93]}
Click me to see the sample solution


52. Extract List of Values from List of Dictionaries

Write a Python program to extract a list of values from a given list of dictionaries.

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]
Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]
Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
Click me to see the sample solution


53. Find Length of a Dictionary

Write a Python program to find the length of a dictionary of values.

Click me to see the sample solution


54. Get Depth of a Dictionary

Write a Python program to get the depth of a dictionary.

Click me to see the sample solution


55. Access Dictionary Key's Element by Index

Write a Python program to access dictionary key's element by index.

Expected Output:
physics
math
chemistry
Click me to see the sample solution


56. Convert Dictionary into a List of Lists

Write a Python program to convert a dictionary into a list of lists.

Original Dictionary:
{1: 'red', 2: 'green', 3: 'black', 4: 'white', 5: 'black'}
Convert the said dictionary into a list of lists:
[[1, 'red'], [2, 'green'], [3, 'black'], [4, 'white'], [5, 'black']]
Original Dictionary:
{'1': 'Austin Little', '2': 'Natasha Howard', '3': 'Alfred Mullins', '4': 'Jamie Rowe'}
Convert the said dictionary into a list of lists:
[['1', 'Austin Little'], ['2', 'Natasha Howard'], ['3', 'Alfred Mullins'], ['4', 'Jamie Rowe']]
Click me to see the sample solution


57. Filter Even Numbers from Dictionary Values

Write a Python program to filter even numbers from a dictionary of values.

Original Dictionary:
{'V': [1, 4, 6, 10], 'VI': [1, 4, 12], 'VII': [1, 3, 8]}
Filter even numbers from said dictionary values:
{'V': [4, 6, 10], 'VI': [4, 12], 'VII': [8]}
Original Dictionary:
{'V': [1, 3, 5], 'VI': [1, 5], 'VII': [2, 7, 9]}
Filter even numbers from said dictionary values:
{'V': [], 'VI': [], 'VII': [2]}
Click me to see the sample solution


58. Get All Combinations of Key-Value Pairs in a Dictionary

Write a Python program to get all combinations of key-value pairs in a given dictionary.

Original Dictionary:
{'V': [1, 4, 6, 10], 'VI': [1, 4, 12], 'VII': [1, 3, 8]}
Combinations of key-value pairs of the said dictionary:
[{'V': [1, 4, 6, 10], 'VI': [1, 4, 12]}, {'V': [1, 4, 6, 10], 'VII': [1, 3, 8]}, {'VI': [1, 4, 12], 'VII': [1, 3, 8]}]
Original Dictionary:
{'V': [1, 3, 5], 'VI': [1, 5]}
Combinations of key-value pairs of the said dictionary:
[{'V': [1, 3, 5], 'VI': [1, 5]}]
Click me to see the sample solution


59. Find Specified Number of Maximum Values in a Dictionary

Write a Python program to find the specified number of maximum values in a given dictionary.

Original Dictionary:
{'a': 5, 'b': 14, 'c': 32, 'd': 35, 'e': 24, 'f': 100, 'g': 57, 'h': 8, 'i': 100}
1 maximum value(s) in the said dictionary:
['f']
2 maximum value(s) in the said dictionary:
['f', 'i']
5 maximum value(s) in the said dictionary:
['f', 'i', 'g', 'd', 'c']
Click me to see the sample solution


60. Find the Shortest List of Values for the Keys in a Dictionary

Write a Python program to find the shortest list of values for the keys in a given dictionary.

Original Dictionary: {'V': [10, 12], 'VI': [10], 'VII': [10, 20, 30, 40], 'VIII': [20], 'IX': [10, 30, 50, 70], 'X': [80]} Shortest list of values with the keys of the said dictionary: ['VI', 'VIII', 'X']
Click me to see the sample solution


61. Count Frequency of a Dictionary's Values

Write a Python program to count the frequency of a dictionary.

Original Dictionary:
{'V': 10, 'VI': 10, 'VII': 40, 'VIII': 20, 'IX': 70, 'X': 80, 'XI': 40, 'XII': 20}
Count the frequency of the said dictionary:
Counter({10: 2, 40: 2, 20: 2, 70: 1, 80: 1})
Click me to see the sample solution


62. Extract Values from Dictionary and Create a List of Lists

Write a Python program to extract values from a given dictionary and create a list of lists from those values.

Original Dictionary:
[{'student_id': 1, 'name': 'Jean Castro', 'class': 'V'}, {'student_id': 2, 'name': 'Lula Powell', 'class': 'V'}, {'student_id': 3, 'name': 'Brian Howell', 'class': 'VI'}, {'student_id': 4, 'name': 'Lynne Foster', 'class': 'VI'}, {'student_id': 5, 'name': 'Zachary Simon', 'class': 'VII'}]
Extract values from the said dictionarie and create a list of lists using those values:
[[1, 'Jean Castro', 'V'], [2, 'Lula Powell', 'V'], [3, 'Brian Howell', 'VI'], [4, 'Lynne Foster', 'VI'], [5, 'Zachary Simon', 'VII']]
[[1, 'Jean Castro'], [2, 'Lula Powell'], [3, 'Brian Howell'], [4, 'Lynne Foster'], [5, 'Zachary Simon']]
[['Jean Castro', 'V'], ['Lula Powell', 'V'], ['Brian Howell', 'VI'], ['Lynne Foster', 'VI'], ['Zachary Simon', 'VII']]
Click me to see the sample solution


63. Convert List of Lists to a Dictionary

Write a Python program to convert a given list of lists to a dictionary.

Original list of lists:
[[1, 'Jean Castro', 'V'], [2, 'Lula Powell', 'V'], [3, 'Brian Howell', 'VI'], [4, 'Lynne Foster', 'VI'], [5, 'Zachary Simon', 'VII']]
Convert the said list of lists to a dictionary:
{1: ['Jean Castro', 'V'], 2: ['Lula Powell', 'V'], 3: ['Brian Howell', 'VI'], 4: ['Lynne Foster', 'VI'], 5: ['Zachary Simon', 'VII']}
Click me to see the sample solution


64. Create Key-Value List Pairings within a Dictionary

Write a Python program that creates key-value list pairings within a dictionary.

Original dictionary:
{1: ['Jean Castro'], 2: ['Lula Powell'], 3: ['Brian Howell'], 4: ['Lynne Foster'], 5: ['Zachary Simon']}
A key-value list pairings of the said dictionary:
[{1: 'Jean Castro', 2: 'Lula Powell', 3: 'Brian Howell', 4: 'Lynne Foster', 5: 'Zachary Simon'}]
Click me to see the sample solution


65. Total Length of All Values in Dictionary (String Values)

Write a Python program to get the total length of all values in a given dictionary with string values.

Original dictionary:
{'#FF0000': 'Red', '#800000': 'Maroon', '#FFFF00': 'Yellow', '#808000': 'Olive'}
Total length of all values of the said dictionary with string values:
20
Click me to see the sample solution


66. Check if Specific Key and Value Exist in Dictionary

Write a Python program to check if a specific key and a value exist in a dictionary.

Original dictionary:
[{'student_id': 1, 'name': 'Jean Castro', 'class': 'V'}, {'student_id': 2, 'name': 'Lula Powell', 'class': 'V'}, {'student_id': 3, 'name': 'Brian Howell', 'class': 'VI'}, {'student_id': 4, 'name': 'Lynne Foster', 'class': 'VI'}, {'student_id': 5, 'name': 'Zachary Simon', 'class': 'VII'}]
Check if a specific Key and a value exist in the said dictionary:
True
True
True
False
False
False
Click me to see the sample solution


67. Invert Dictionary with Non-Unique Values

Write a Python program to invert a given dictionary with non-unique hashable values.

Sample Output:
{8: ['Ora Mckinney', 'Mathew Gilbert'], 7: ['Theodore Hollandl', 'Mae Fleming', 'Ivan Little']}
Click me to see the sample solution


68. Combine Dictionaries Creating List of Values per Key

Write a Python program to combine two or more dictionaries, creating a list of values for each key.

Sample Output:
Original dictionaries:
{'w': 50, 'x': 100, 'y': 'Green', 'z': 400}
{'x': 300, 'y': 'Red', 'z': 600}
Combined dictionaries, creating a list of values for each key:
{'w': [50], 'x': [100, 300], 'y': ['Green', 'Red'], 'z': [400, 600]}
Click me to see the sample solution


69. Group Elements of a List Based on a Function

Write a Python program to group the elements of a given list based on the given function.

Sample Output:
Original list & function:
[7, 23, 3.2, 3.3, 8.4] Function name: floor:
Group the elements of the said list based on the given function:
{7: [7], 23: [23], 3: [3.2, 3.3], 8: [8.4]}
Original list & function:
['Red', 'Green', 'Black', 'White', 'Pink'] Function name: len:
Group the elements of the said list based on the given function:
{3: ['Red'], 5: ['Green', 'Black', 'White'], 4: ['Pink']}
Click me to see the sample solution


70. Map List Values to Dictionary Using a Function

Write a Python program to map the values of a given list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value.

Sample Output:
{1: 1, 2: 4, 3: 9, 4: 16}
Click me to see the sample solution


71. Retrieve Nested Value by Selector List

Write a Python program to retrieve the value of the nested key indicated by the given selector list from a dictionary or list.

Sample Output:
Russell
2
Click me to see the sample solution


72. Invert Dictionary with Unique Values

Write a Python program to invert a dictionary with unique hashable values.

Sample Output:
{10: 'Theodore', 11: 'Mathew', 9: 'Roxanne'}
Click me to see the sample solution


73. Extract List of Values from List of Dictionaries

Write a Python program to convert a list of dictionaries into a list of values corresponding to the specified key.

Sample Output:
Original list of dictionaries:
[{'name': 'Theodore', 'age': 18}, {'name': 'Mathew', 'age': 22}, {'name': 'Roxanne', 'age': 20}, {'name': 'David', 'age': 18}]
Convert a list of dictionaries into a list of values corresponding to the specified key:
[18, 22, 20, 18]
Click me to see the sample solution


74. Create Dictionary with Same Keys and Function-Transformed Values

Write a Python program to create a dictionary with the same keys as the given dictionary and values generated by running the given function for each value.

Sample Output:
Original dictionary elements:
{'Theodore': {'user': 'Theodore', 'age': 45}, 'Roxanne': {'user': 'Roxanne', 'age': 15}, 'Mathew': {'user': 'Mathew', 'age': 21}}
Dictionary with the same keys:
{'Theodore': 45, 'Roxanne': 15, 'Mathew': 21}
Click me to see the sample solution


75. Find All Keys with a Given Value

Write a Python program to find all keys in a dictionary that have the given value.

Sample Output:
Original dictionary elements:
{'Theodore': 19, 'Roxanne': 20, 'Mathew': 21, 'Betty': 20}
Find all keys in the said dictionary that have the specified value:
['Roxanne', 'Betty']
Click me to see the sample solution


76. Combine Two Lists into a Dictionary

Write a Python program to combine two lists into a dictionary. The elements of the first one serve as keys and the elements of the second one serve as values. Each item in the first list must be unique and hashable.

Sample Output:
Original lists:
['a', 'b', 'c', 'd', 'e', 'f']
[1, 2, 3, 4, 5]
Combine the values of the said two lists into a dictionary:
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
Click me to see the sample solution


77. Transform Dictionary into List of Tuples

Write a Python program to transform a dictionary into a list of tuples.

Sample Output:
Original Dictionary:
{'Red': 1, 'Green': 3, 'White': 5, 'Black': 2, 'Pink': 4}
Convert the said dictionary to a list of tuples:
[('Red', 1), ('Green', 3), ('White', 5), ('Black', 2), ('Pink', 4)]
Click me to see the sample solution


78. Create Flat List of All Keys in a Dictionary

Write a Python program to create a flat list of all the keys in a flat dictionary.

Sample Output:
Original dictionary elements:
{'Theodore': 19, 'Roxanne': 20, 'Mathew': 21, 'Betty': 20}
Create a flat list of all the keys of the said flat dictionary:
['Theodore', 'Roxanne', 'Mathew', 'Betty']
Click me to see the sample solution


79. Create Flat List of All Values in a Dictionary

Write a Python program to create a flat list of all the values in a flat dictionary.

Sample Output:
Original dictionary elements:
{'Theodore': 19, 'Roxanne': 20, 'Mathew': 21, 'Betty': 20}
Create a flat list of all the values of the said flat dictionary:
[19, 20, 21, 20]
Click me to see the sample solution


80. Find Key of Maximum Value in a Dictionary

Write a Python program to find the key of the maximum value in a dictionary.

Sample Output:
Original dictionary elements:
{'Theodore': 19, 'Roxanne': 22, 'Mathew': 21, 'Betty': 20}
Finds the key of the maximum and minimum value of the said dictionary:
('Roxanne', 'Theodore')
Click me to see the sample solution


Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.