Python Dictionary Practice Programs, Exercises

Python dictionary programs are used to create dictionaries as well as to search for specific entries in them. Dictionaries can be created by assigning key-value pairs for each entry in the program code. This dictionary program in python for practice help user to get expertise in dictionary fundamentals.

1). Python Dictionary program to add elements to the dictionary.

2). Python Dictionary program to print the square of all values in a dictionary.
Input : {‘a’: 5, ‘b’:3, ‘c’: 6, ‘d’ : 8}
Output :
a : 25
b : 9
c : 36
d : 64

3). Python Dictionary program to move items from dict1 to dict2.
Input :
dict1 = {‘name’: ‘john’, ‘city’: ‘Landon’, ‘country’: ‘UK’}
dict2 = {}
Output :
dict1 = {}
dict2 = {‘name’: ‘john’, ‘city’: ‘Landon’, ‘country’: ‘UK’}

4). Python Dictionary program to concatenate two dictionaries.
Input :
dict1 = {‘Name’: ‘Harry’, ‘Rollno’:345, ‘Address’: ‘Jordan’}
dict2 = {‘Age’ : 25, ‘salary’: ‘$25k’}
Output :
dict1 = {‘Name’: ‘Harry’, ‘Rollno’:345, ‘Address’: ‘Jordan’, ‘Age’ : 25, ‘salary’: ‘$25k’}

5)Python Dictionary program to get a list of odd and even keys from the dictionary.
Input :
{1: 25, 5:’abc’, 8:’pqr’, 21:’xyz’, 12:’def’, 2:’utv’}
Output :
Even Key = [[8, ‘pqr’], [12, ‘def’], [2, ‘utv’]]
Odd Key = [[1, 25], [5, ‘abc’], [21, ‘xyz’]]

6). Python Dictionary Program to create a dictionary from two lists.
Input :
list1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]|
list2 = [12, 23, 24, 25, 15, 16]
Output :
{‘a’: 12, ‘b’: 23, ‘c’: 24, ‘d’: 25, ‘e’: 15}

7). Python Dictionary program to store squares of even and cubes of odd numbers in a dictionary using dictionary comprehension.
Input :
[4, 5, 6, 2, 1, 7, 11]
Output :
{4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}

8). Python Dictionary program to clear all items from the dictionary.

9). Python Dictionary program to remove duplicate values from Dictionary.
Input :
{‘a’: 12, ‘b’: 2, ‘c’: 12, ‘d’: 5, ‘e’: 35, ‘f’: 5}
Output :
{‘a’: 12, ‘b’: 2, ‘d’: 5, ‘e’: 35}

10). Python Dictionary program to create a dictionary from the string.
Input  = ‘SQATools’
Output{‘S’: 1, ‘Q’: 1, ‘A’: 1, ‘T’: 1, ‘o’: 2, ‘l’: 1, ‘s’: 1}

11). Python Dictionary program to sort a dictionary using keys.
Input = {‘d’ : 21, ‘b’ : 53,  ‘a’: 13, ‘c’: 41}
Output =
(‘a’, 13)
(‘b’, 53)
(‘c’, 41)
(‘d’, 21)

12). Python Dictionary program to sort a dictionary in python using values.
Input = {‘d’ : 14, ‘b’ : 52,  ‘a’: 13, ‘c’: 1 }
Output= (c, 1) (a,13) (d, 14) (b, 52)

13). Python Dictionary program to add a key in a dictionary.
Input= {1:’a’, 2:’b’}
Output= (1:’a’, 2:’b’, 3:’c’}

14). Python Dictionary program to concatenate two dictionaries.
Input:
D1 = {‘name’ : ’yash’, ‘city’ :  ‘pune’}
D1 = {‘course’ : ’python’, ‘institute’ : ’sqatools’}
Output :
{ ‘name’ : ’yash’, city: ‘pune’, ‘course’ : ’python’, ‘institute’ : ’sqatools’ }

15). Python Dictionary program to swap the values of the keys in the dictionary.
Input = {name:’yash’, city: ‘pune’}
Output = {name:’pune’, city: ‘yash’}

16). Python Dictionary program to get the sum of all the items in a dictionary.
Input = {‘x’ : 23, ‘y’ : 10 , ‘z’ : 7}
Output = 40

17). Python program to get the size of a dictionary in python.
Hint : use sys.getsizeof(var) method.
Input = {‘name’ : ’virat’, ‘sport’ : ’cricket’}
Output = 232bytes

18). Python Dictionary program to check whether a key exists in the dictionary or not.
Input:
Dict1 = {city:’pune’, state=’maharashtra’}
Dict1[country]
Output= ‘key does not exist in dictionary

19). Python program to iterate over a dictionary.
Input :
Dict1 = {food:’burger’, type:’fast food’}
Output :
food : burger
type : fast food

20). Python Dictionary program to create a dictionary in the form of (n^3) i.e. if key=2 value=8
Input: n=4
Output ={1 : 1, 2 : 8, 3 : 27, 4 : 64}

21). Python Dictionary program to insert a key at the beginning of the dictionary.
Input = { ‘course’ : ’python’,  ‘institute’ : ’sqatools’ }
Insert : ( ‘name’ : ’omkar’ )
Output= { ‘name’ : ’omkar’, ‘course’ : ’python’, ‘institute’ : ’sqatools’}

22). Python Dictionary  program to create a dictionary where keys are between 1 to 5 and values are squares of the keys.
Output ={1 : 1, 2 : 4, 3 : 9, 4 : 16, 5 : 25}

23). Python Dictionary program to find the product of all items in the dictionary.
Input = { ‘a’ : 2, ‘b’ : 4, ‘c’ : 5}
Output = 40

24). Python Dictionary program to remove a key from the dictionary.
Input = {a:2,b:4,c:5}
Output = (a:1,b:4}

25). Python Dictionary program to map two lists into a dictionary.
Input
a = [ ‘name’, ‘sport’, ‘rank’, ‘age’]
b = [‘Virat’, ‘cricket’, 1,  32]
Output =  { ‘name’ : ’virat’, ‘sport’ : ’cricket’, ‘rank’: 1, ‘age’ : 32}

26). Python Dictionary program to find maximum and minimum values in a dictionary.
Input :
Dict = { ‘a’ : 10, ‘b’ : 44 , ‘c’ : 60, ‘d’ : 25}
Output :
Maximum value: 60
Minimum value: 10

27). Python Dictionary program to group the same items into a dictionary value.
Input :
list = [1,3,4,4,2,5,3,1,5,5,2,]
Output = {1 : [1, 1], 2 :[2, 2], 3 : [3, 3], 4 : [4, 4], 5 : [5, 5, 5]}

28). Python Dictionary program to replace words in a string using a dictionary.
String = ’learning python at sqa-tools’
Dict = { ‘at’ : ’is’, ‘sqa-tools’ : ’fun’}
Output = ‘learning python is fun’

29). Python Dictionary program to remove a word from the string if it is a key in a dictionary.
String = ’sqatools is best for learning python’
Dict = { ‘best’ : 2, ‘learning’ : 6}
Output = “sqatools is for python”

30). Python Dictionary program to remove duplicate values from dictionary values.
Input:
Dict1 = { ‘marks1’ : [23,28,23,69], ‘marks2’ : [ 25, 14,25] }
Output= { ‘marks1’ : [28, 69], ‘marks2’ : [14,19] }

31). Python Dictionary program to check whether a dictionary is empty or not.
Input:
Dict1 = {}
Output: Given dictionary is empty

32). Python Dictionary program to add two dictionaries if the keys are the same then add their value.
Input:
Dict1 = { ‘x’:10, ‘y’:20, ‘c’:50, ‘f’:44 }
Dict2 = {‘x’:60,’c’:25,’y’:56}
Output = {‘x’: 70, ‘c’: 75, ‘y’: 76}

33). Python Dictionary program to print all the unique values in a dictionary.
Input :
Dict1 = [{name1:’robert’},  {name2:’john’}, {name3:’jim’}, {name4:’robert’}]
Output = [‘robert’, ’john’, ’jim’]

34). Python Dictionary program to display different combinations of letters from dictionary values.
Input:
Dict1 = { x:[e,f], y:[a,b]}
Output: 
  ea
  eb
  fa
  fb

35). Python Dictionary program to create a dictionary from a string.
Input = ‘sqatools’
Output = {s:2,q:1,a:1, t:1,o:2, l:1}

36). Python Dictionary program to print the given dictionary in the form of tables.
Input:
Dict1= {names:[‘virat’,’messi’,’kobe’], sport:[‘cricket’,’football’,’basketball’]}
Output:
     Names   sport
0    Virat      cricket
1    Messi     football
2    Kobe      basketball

37). Python program to count frequencies in a list using a dictionary.
Input:
list1= [2,5,8,1,2,6,8,5]
Output = {1:1,2:2, 5:2, 6:1, 8:2}

38). Python program to find mean of values of keys in a dictionary.
Input :
Dict1= {m1:25, m2:20, m3:15}
Output :
Mean is 20

39). Python program to convert a list into a nested dictionary of keys.
Input = [a,b,c,d]
Output = {a: {b: {c: {d: {}}}}}

40). Python program to sort a list of values in a dictionary.
Input= { a1 : [1,5,3], a2 : [10,6,20] }
Output= ( a1 : [1,3,5], a2 : [6,10,20] }

41). Python program to get a product with the highest price from a dictionary.
Input = { ‘price1’ : 450, ‘price2‘ : 600,  ‘price3′ : 255,  ‘price4′ : 400}
Output = P2 500

42). Python program to print a dictionary line by line.
Input = {‘virat’: {sport:’cricket’, team:’india’}, ‘messi’: {sport:’football’, team:’argentina’}}
Output=
Virat
Sport : cricket
Team : india

Messi
Sport : football
Team : argentina

43). Python program to convert a key value list dictionary into a list of list.
Input = {sqa:[1,4,6], tools:[3,6,9]}
Output= [[sqa,1,4,6],[tools,3,6,9]]

44). Python program to convert a list of dictionaries to a list of lists.
Input= [{‘sqa’:123,’tools’:456}]
Output= [[sqa],[tools],[123],[456]]

45). Python program to count a number of items in a dictionary value that is in a list.
Input = {‘virat’:[‘match1’,match2’,’match3’], ‘rohit’:[‘match1’,’match2’]}
Output= 5

46). Python program to sort items in a dictionary in descending order.
Input = {‘Math’:70, ‘Physics’:90, ‘Chemistry’:67}
Output = {‘Physics’:90, ’Maths’:70, ’Chemistry’:67}

47). Python program to replace dictionary values with their average.
Input = { name:’ketan’, subject:’maths’, p1:80, p2:70}
Output = { name:’ketan’,subject:’maths’, p1+p2:75}

48). Python program to match key values in two dictionaries.
Input
A = {‘k1’: p, ‘k2’: q, ‘k3’: r}
B = {‘k1’: p, ‘k2’: s}

Output = k1: p is present in both A and B

49). Python program to create a dictionary of keys a, b, and c where each key has as value a list from 1-5, 6-10, and 11-15 respectively. 
Output = { a: [1,2,3,4,5],  b: [6,7,8,9,10],  c: [11,12,13,14,15] }

50). Python program to drop empty Items from a given dictionary.
Input = {‘m1’:40, ‘m2’:50, ‘m3’:None}
Output = {‘m1’:40, ‘m2’:50}

51). Python program to filter a dictionary based on values.
Input{ ‘alex’ : 50,  ‘john’ : 45, ‘Robert’ : 30}
Output= value greater than 40
{alex:50, john:45}

52). Python program to convert a key-values list to a flat dictionary.
Input = {‘name’: [‘Apr’, ‘May’, ‘June’], ‘month’: [4, 5, 6]}
Output ={‘Apr’: 4, ‘May’: 5, ‘June’: 6}

53). Python program to convert a list of Tuples into a dictionary
Input =  [(“mike”, 1), (“Sarah”, 20), (“Jim”, 16)]
Output{“mike”:1, “Sarah”:20, “Jim”:16}

54). Python program to convert string to the dictionary.
Input:
str1= “Apr=April; Mar=March”
Output: {‘Apr’: ‘April’, ‘ Mar’: ‘March’}

55). Python program to convert a matrix into a dictionary.
Input = [[1,2,3],[4,5,6]]
Output{1 : [1,2,3] , 2 : [4,5,6]}

56). Python program to check all values are the same in a dictionary.
Input :
A={‘virat’:50, ’rohit’:50, ’rahul’:50, ’hardik’:50}    
OutputTrue

57). Python program to create a dictionary grouping a sequence of key-value pairs into a dictionary of lists.
Input :
A= {‘virat’:50, ’rohit’:40, ’virat’:30, ’rohit’:10}
Output{‘virat’:[50,30],’rohit’:[40,10]}

58). Python program to split a given dictionary of lists into list of dictionaries.
Input :
A={ ‘t20’ : [50,40,30,45], ‘odi’ : [70,10,0,65] }
Output :
[ {t20:50, odi:70} ,{t20:40, odi:10}, {t20:30, odi:0}, {t20:45, odi:65} ]

59). Python program to remove a specified dictionary from a given list.
Input = [ { t20:50, odi:70 }, { t20:40, odi:10 }, { t20:30,odi:0 }, { t20:45, odi:65} ]
Remove 4th dictionary
Output=[ { t20:50, odi:70 }, { t20:40, odi:10 } ,{ t20:30, odi:0 } ]

60). Python program to convert string values of a given dictionary, into integer/float datatypes.
Input :
A = { ‘a’: ’30’,  ‘b’: ’20’, ‘c: ’10’ } 
B=  { ‘a’: ‘3.33’, ‘b’: ‘20.50’, ‘c: ‘12.5’ }
Output
A = { ‘a’: 30, ‘b’: 20, ‘c: 10 } 
B = { ‘a’: 3.33, ‘b’: 20.50, ‘c: 12.5 } 

61).  A Python dictionary contains a list as a value. Python program to clear the list values in the said dictionary.
Input={‘virat’:[50,30],’rohit’:[40,10]} 
Output{ ‘virat’: [], ’rohit’: [] }

62). A Python dictionary contains list as value. Python program to update the list values in the said dictionary.
Input{ ‘virat’ : [ 50,30 ], ’rohit’ : [ 40,10 ] }
Output ={‘virat’: [60, 40], ‘rohit’: [15, -15]}

63). Python program to extract a list of values from a given list of dictionaries
Input:
A=[ { t20:50, odi:70 }, { t20:40, odi:10 }, { t20:30, odi:0 }, { t20:45, odi:65 } ]
Extract values for ‘odi’
Output = [70,10,0,65]

64). Python program to find the length of dictionary values.
Input = { 1:’sqa’, 2:’tools’, 3:’python’ }
Output{ ‘sqa’:3, ’tools’:5, ‘python’:6 }

65). Python program to get the depth of the dictionary.
Input = {‘a’:1, ‘b’: {‘c’: {‘d’: {}}}}
Output4

66). Python program to create nested Dictionary using List.
Input = {‘sqatools’:8,’python’:6} list=[1,2]
Output{ 1: { sqatools’:8 }, 2:[‘python’:6] }

67). Python program to extract key’s value if key is present in list and dictionary
Input:
A = [‘sqatools’,’is’,’best’]
B = {‘sqatools’:10}
Output = 10

68). Python program to remove keys with values greater than n.
Input { ‘sqa’:3, ’tools’:5, ‘python’:7 } 
n=6
Output{‘sqa’:3,’tools’:5}

69). Python program to remove keys with substring values.
Input :
D1 = { 1:’sqatools is best’, 2: ’for learning python’}
Substr = [‘best’,’ excellent’]
Output{ 2:  ’for learning python’ }

70). Python program to access the dictionary key.
Input :
Drinks = { pepsi:50, sprite:60, slice:55}
Output:
Pepsi
Sprite
Slice

71). Python program to filter even numbers from a given dictionary value.
Input = { ‘a’: [11, 4, 6, 15],  ‘b’: [3, 8, 12],  ‘c’: [5, 3, 10] }
Output{ ‘a’:[4,6], ’b’:[8,12],  ’c’:[10] }

72). Python program to find the keys of maximum values in a given dictionary.
Input = { a:18, b:50, c:36, d:47, e:60 }
Find keys of first 2 max values from the dictionary
Output[ e, b ]

73). Python program to find the shortest list of values with the keys in a given dictionary.
Input{ ‘a’: [10, 12], ‘b’: [10],  ‘c’: [10, 20, 30, 40], ‘d’: [20] }
Output[ ‘b’, ’d’ ]

74). Python program to count the frequency in a given dictionary.
Input = { a:10, b:20, c:25, d:10, e:30, f:20 }
Output{ 10:2, 20:2, 25:1, 30:1}

75). Python program to create key-value list pairings in a given dictionary.
Input{ 1: [‘Virat Kohli’], 2: [‘Rohit Sharma’], 3: [‘Hardik Pandya’] }
Output[ { 1: ‘Virat Kohli’, 2: ‘Rohit Sharma’, 3: ‘Hardik Pandya’ } ]

76). Python program to get the total length of a dictionary with string values.
Input = { ‘virat’:50,’Rohit’:40,’Rahul’:25 }
Output :
Total length: 6

77). Python program to group the elements of a given list based on the given function.
Hint : Function name: len()
Input[‘abc’, ‘defg’, ‘hijkl’]
Output{ 3:[‘abc’],  4:[‘defg’], 5:[‘hijkl’] }

78). Python program to retrieve the value of the nested key indicated by the given selector list from a dictionary or list.
Input = {‘p1’:{name:{first:lionel, last:messi}, team:[psg,argentina]}}
Output :
Messi
Argentina

79). Python program to show a dictionary with a maximum count of pairs.
Input:
A = {name:1,age:2}
B = {name:1,age:2,course:3,institute:4}
Output:
2nd dictionary has maximum keys, 4

80). Python program to Extract Unique values from dictionary values.
Input = { sqa:[1,2,5,6],tools:[3,8,9],is:[2,5,0],best:[3,6,8] }
Output[0,1,2,3,5,6,8,9]

81). Python program to show keys associated with values in dictionary
Input = { ‘xyz’:[20,40], abc:[10,20] }
Output ={20: [‘xyz’], 40: [‘xyz’], 10: [‘abc’], 30: [‘abc’]}

82). Python program to convert a list of dictionaries into a list of values corresponding to the specified key.
Input = [ { name:’jos’,  age:30 }, { name:’david’, age:25 }, { name:’virat’, age:32 } ]
Output = [30, 25, 32]

83). Python program to find all keys in the provided dictionary that have the given value.
Input = {‘a’: 19, ‘b’: 20, ‘c’: 21, ‘d’: 20}
value = 20
Output = [‘b’, ‘d’]

84). Python program to convert given a dictionary to a list of tuples.
Input = {‘a’: 19, ‘b’: 20, ‘c’: 21, ‘d’: 20}
Output =[ (a,19), (b,20), (c,21), (d,20) ]

85). Python program to create a flat list of all the keys in a flat dictionary.
Input = { ‘sqa’: [1,2,5,6], ‘tools’: [3,8,9], ‘is’: [2,5,0], ‘best’ : [3,6,8] } 
Output = [ sqa, tools, is, best]

86). Python program to create a flat list of all the values in a flat dictionary. 
Input = { sqa:1, tools:2, is:2, best:4 } 
Output =[ 1,2,3,4 ]

87). Python program to initialize a dictionary with default values.
Name = [ ”Virat”, ”Rohit” ]
Defaults = { ‘sport’ : ’cricket’, ‘salary’ : 100000 }
Output = { “Virat” : { sport: ’cricket’, salary:100000 }, “Rohit”:{ sport: ’cricket’, salary:10000}}

88). Python program to delete a list of keys from a dictionary.
Input = { ‘a’: 19, ‘b’: 20, ‘c’: 21, ‘d’: 20, ‘e’: 50 }
Keys to be removed:  [ ‘a’, ‘d’, ‘e’ ]
Output = { ‘b’: 20, ‘c’: 21 }

89). Python program to rename key of a dictionary
Input = { ‘a’: 19, ‘b’: 20, ‘c’: 21, ‘d’: 20}
Output = { ‘a’: 19, ‘b’: 20, ‘c’: 21, ‘e’: 20}

90). Python program to Invert a given dictionary with non-unique hashable values.
Input = { ‘alex’:1, ’bob’:2,’ martin’:1, ’robert’:2 }
Output = { 1:[‘alex’,’marin’], 2:[‘bob’,’robert’] }

91). Python program to Sort Dictionary by values summation
Input = { x:[1,5,6], y:[4,8,2], c:[3,9] }
Output = { c:12, x:12, y:14}

92). Python program to convert a dictionary into n sized dictionary.
Input = { a:1, b:2, c:3, d:4, e:5, f:6 }
N = 3
Output = [{ a:1, b:2, c:3 }, {d:4, e:5, f:6}]

93). Python program to Sort dictionaries list by Key’s Value list index
Input = [ { a:[ 6, 7, 8],  b:9, c : 10 }, { a : [4, 6, 9],  b: 16, c : 1 } ]
Key = a,  index=0
Output = [ { a : [4, 6, 9],  b: 16, c : 1}, {a: [ 6, 7, 8],  b:9, c : 10} ]…

94) Python program to reverse each string value in the dictionary and add an underscore before and after the Keys.
Input  = {“a” : “Python”, “b”: “Programming”, “c”: “Learning”}
Output = {“_a_”: “nythonP”, “_b_” : “gnimmargorP”, “_c_”: “gearninL”}

95). Python program to sum unique elements from dictionary list values.
Input = { ‘a’ : [ 6, 7, 2, 8, 1], ‘b’ : [2, 3, 1, 6, 8, 10], ‘d’ : [1, 8, 2, 6, 9] }
Output :
46

Leave a Comment