w3resource logo


Python Exercises

Python Data Types: Dictionary - Exercises, Practice, Solution

Secondary Nav

Python Dictionary [ 39 exercises with solution]

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

1. Write a Python script to sort (ascending and descending) a dictionary by value. Go to the editor

Click me to see the sample solution

2. Write a Python script to add a key to a dictionary. Go to the editor

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

Click me to see the sample solution

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

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. Write a Python script to check if a given key already exists in a dictionary. Go to the editor

Click me to see the sample solution

5. Write a Python program to iterate over dictionaries using for loops. Go to the editor

Click me to see the sample solution

6. Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x). Go to the editor
Sample Dictionary ( n = 5) :
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Click me to see the sample solution

7. Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both included) and the values are square of keys. Go to the editor
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. Write a Python script to merge two Python dictionaries. Go to the editor
Click me to see the sample solution

9. Write a Python program to iterate over dictionaries using for loops. Go to the editor
Click me to see the sample solution

10. Write a Python program to sum all the items in a dictionary. Go to the editor
Click me to see the sample solution

11. Write a Python program to multiply all the items in a dictionary. Go to the editor
Click me to see the sample solution

12. Write a Python program to remove a key from a dictionary. Go to the editor
Click me to see the sample solution

13. Write a Python program to map two lists into a dictionary. Go to the editor
Click me to see the sample solution

14. Write a Python program to sort a dictionary by key. Go to the editor
Click me to see the sample solution

15. Write a Python program to get the maximum and minimum value in a dictionary. Go to the editor
Click me to see the sample solution

16. Write a Python program to get a dictionary from an object's fields. Go to the editor
Click me to see the sample solution

17. Write a Python program to remove duplicates from Dictionary. Go to the editor
Click me to see the sample solution

18. Write a Python program to check a dictionary is empty or not. Go to the editor
Click me to see the sample solution

19. Write a Python program to combine two dictionary adding values for common keys. Go to the editor
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. Write a Python program to print all unique values in a dictionary. Go to the editor
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. Write a Python program to create and display all combinations of letters, selecting each letter from a different key in a dictionary. Go to the editor
Sample data : {'1':['a','b'], '2':['c','d']}
Expected Output:
ac
ad
bc
bd
Click me to see the sample solution

22. Write a Python program to find the highest 3 values in a dictionary. Go to the editor
Click me to see the sample solution

23. Write a Python program to combine values in python list of dictionaries. Go to the editor
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. Write a Python program to create a dictionary from a string. Go to the editor
Note: Track the count of the letters from the string.
Sample string : 'w3resource'
Expected output: {'3': 1, 's': 1, 'r': 2, 'u': 1, 'w': 1, 'c': 1, 'e': 2, 'o': 1}
Click me to see the sample solution

25. Write a Python program to print a dictionary in table format. Go to the editor
Click me to see the sample solution

26. Write a Python program to count the values associated with key in a dictionary. Go to the editor
Sample data: = [{'id': 1, 'success': True, 'name': 'Lary'}, {'id': 2, 'success': False, 'name': 'Rabi'}, {'id': 3, 'success': True, 'name': 'Alex'}]
Expected result: Count of how many dictionaries have success as True
Click me to see the sample solution

27. Write a Python program to convert a list into a nested dictionary of keys. Go to the editor
Click me to see the sample solution

28. Write a Python program to sort a list alphabetically in a dictionary. Go to the editor
Click me to see the sample solution

29. Write a Python program to remove spaces from dictionary keys. Go to the editor
Click me to see the sample solution

30. Write a Python program to get the top three items in a shop. Go to the editor
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. Write a Python program to get the key, value and item in a dictionary. Go to the editor
Click me to see the sample solution

32. Write a Python program to print a dictionary line by line. Go to the editor
Click me to see the sample solution

33. Write a Python program to check multiple keys exists in a dictionary. Go to the editor
Click me to see the sample solution

34. Write a Python program to count number of items in a dictionary value that is a list. Go to the editor
Click me to see the sample solution

35. Write a Python program to sort Counter by value. Go to the editor
Sample data : {'Math':81, 'Physics':83, 'Chemistry':87}
Expected data: [('Chemistry', 87), ('Physics', 83), ('Math', 81)]
Click me to see the sample solution

36. Write a Python program to create a dictionary from two lists without losing duplicate values. Go to the editor
Sample lists: ['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII'], [1, 2, 2, 3]
Expected Output: defaultdict(<class 'set'>, {'Class-VII': {2}, 'Class-VI': {2}, 'Class-VIII': {3}, 'Class-V': {1}})
Click me to see the sample solution

37. Write a Python program to replace dictionary values with their sum. Go to the editor
Click me to see the sample solution

38. Write a Python program to convert a dictionary to OrderedDict. Go to the editor
Click me to see the sample solution

39. Write a Python program to match key values in two dictionaries. Go to the editor
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

Python Code Editor:





More to Come !



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables