0% found this document useful (0 votes)
15 views3 pages

Python Dictionary Exercises and Solutions

Python exercise

Uploaded by

mehurvala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Python Dictionary Exercises and Solutions

Python exercise

Uploaded by

mehurvala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.
[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.

2. 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}

3. 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}

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

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

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).
Sample Dictionary ( n = 5) :
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

7. 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}

Object 1
8. Write a Python script to merge two Python dictionaries.

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

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

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

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

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

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

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

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

17. Write a Python program to remove duplicates from the dictionary.

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

19. 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})

20. 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'}
21. 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
[Link] a Python program to find the highest 3 values of corresponding keys in a dictionary.
23. 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})
24. 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}

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

26. Write a Python program to count the values associated with a key in a dictionary.
Expected Output:
6
2

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

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

29. Write a Python program to remove spaces from dictionary keys.

30. 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

You might also like