0% found this document useful (0 votes)
275 views6 pages

Assignment Dict For Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Dictionary

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 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 if 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 square of 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}

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 dictionary by key. 

15. Write a Python program to get the maximum and minimum value in 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 Dictionary. 

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


19. Write a Python program to combine two dictionary 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 unique 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

22. Write a Python program to find the highest 3 values in a dictionary. 

23. Write a Python program to combine values in python 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: {'3': 1, 's': 1, 'r': 2, 'u': 1, 'w': 1, 'c': 1, 'e': 2, 'o': 1}
25. Write a Python program to print a dictionary in table format. 

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


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

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

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

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

33. Write a Python program to check multiple keys exists in a dictionary. 

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


a list. 
35. 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)]

36. 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-VII': {2}, 'Class-VI': {2}, 'Class-
VIII': {3}, 'Class-V': {1}})

37. Write a Python program to replace dictionary values with their sum. 

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

39. Write a Python program to store a given dictionary 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'}]}

40. 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]

You might also like