Practice Programs on Dictionary
Practice Programs on Dictionary
1). Python Dic onary program to add elements to the dic onary.
2). Python Dic onary program to print the square of all values in a dic onary.
Input : {‘a’: 5, ‘b’:3, ‘c’: 6, ‘d’ : 8}
Output :
a : 25
b:9
c : 36
d : 64
3). Python Dic onary Program to create a dic onary 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}
4). Python Dic onary program to store squares of even and cubes of odd numbers in a dic onary using dic onary
comprehension.
Input :
[4, 5, 6, 2, 1, 7, 11]
Output :
{4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}
5). Python Dic onary program to remove duplicate values from Dic onary.
Input :
{‘a’: 12, ‘b’: 2, ‘c’: 12, ‘d’: 5, ‘e’: 35, ‘f’: 5}
Output :
{‘a’: 12, ‘b’: 2, ‘d’: 5, ‘e’: 35}
6). Python Dic onary program to create a dic onary from the string.
Input = ‘SQATools’
Output = {‘S’: 1, ‘Q’: 1, ‘A’: 1, ‘T’: 1, ‘o’: 2, ‘l’: 1, ‘s’: 1}
7). Python Dic onary program to swap the values of the keys in the dic onary.
Input = {name:’yash’, city: ‘pune’}
Output = {name:’pune’, city: ‘yash’}
8). Python Dic onary program to get the sum of all the items in a dic onary.
Input = {‘x’ : 23, ‘y’ : 10 , ‘z’ : 7}
Output = 40
9). Python Dic onary program to check whether a key exists in the dic onary or not.
Input:
Dict1 = {city:’pune’, state=’maharashtra’}
Dict1[country]
Output= ‘key does not exist in dic onary
10). Python Dic onary program to create a dic onary 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}
11). Python Dic onary program to find maximum and minimum values in a dic onary.
Input :
Dict = { ‘a’ : 10, ‘b’ : 44 , ‘c’ : 60, ‘d’ : 25}
Output :
Maximum value: 60
Minimum value: 10
12). Python Dic onary program to group the same items into a dic onary 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]}
13). Python program to count frequencies in a list using a dic onary.
Input:
list1= [2,5,8,1,2,6,8,5]
Output = {1:1,2:2, 5:2, 6:1, 8:2}
14). Python program to filter even numbers from a given dic onary value.
Input = { ‘a’: [11, 4, 6, 15], ‘b’: [3, 8, 12], ‘c’: [5, 3, 10] }
Output = { ‘a’:[4,6], ’b’:[8,12], ’c’:[10] }
15). Python program to find the keys of maximum values in a given dic onary.
Input = { a:18, b:50, c:36, d:47, e:60 }
Find keys of first 2 max values from the dic onary
Output = [ e, b ]
16). Python program to sum unique elements from dic onary list values.
Input = { ‘a’ : [ 6, 7, 2, 8, 1], ‘b’ : [2, 3, 1, 6, 8, 10], ‘d’ : [1, 8, 2, 6, 9] }
Output :
46
17). Python program to create a frequencies of words dic onary from the string
Input = “Now pass the input to the input to print the output”
Output = {‘Now’: 1, ‘pass’: 1, ‘the’:3, ‘input’:2, ‘to’:2, ‘print’:1, ‘output’1}
18). Python program to create dic onary with factors of each element from of the given tuple
Input = (6, 10, 7, 12)
Output = {6:[1,2,3,6], 10:[1,2,5,10], 7:[1,7], 12:[1,2,3,4,6,12]}