Worksheet - Dictionary
Worksheet - Dictionary
DICTIONARY
Ans:
123
2 What will be the output of following code-
a={i: 'Hi!' + str(i) for i in range(5)}
a
Ans:
Ans:
{0: 1, 1: 1, 2: 1}
4 What will be the output of following code-
a={i: i*i for i in range(6)}
a
Ans:
1|Page
5 What will be the output of following code-
a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
Ans:
3
6 What will be the output of following program:
dictionary = {1:'1', 2:'2', 3:'3'}
del dictionary[1]
dictionary[1] = '10'
del dictionary[2]
print(len(dictionary))
Ans:
2
7 Predict the Output:
Ans:
Key error
8 What will be the output of following program:
dict1 = {"key1":1, "key2":2}
dict2 = {"key2":2, "key1":1}
print(dict1 == dict2)
Ans:
True
9 What will be the output of following program:
dict={"Virat":1,"Rohit":2}
dict.update({"Rahul":2})
2|Page
print(dict)
Ans:
Ans:
Dictionary is empty so nothing will be printed
Ans:
{1: 'check', 2: 'check', 3: 'check'}
Ans:
{'a': 1, 'b': [2, 3, 4]}
13 What will be the output of following program:
a = {}
a[1] = 1
a['1'] = 2
a[1.0]=4
count = 0
for i in a:
3|Page
count += a[i]
print(count)
Ans:
6
Ans:
2
Ans:
0
16 What will be the output of following program:
a={1:"A",2:"B",3:"C"}
del a
Ans:
Nothing will be printed
4|Page
Ans:
123
Ans:
{1: 5, 2: 3}
Ans:
9
Ans:
{1: 'A', 2: 'B', 3: 'C', 4: 'D'}
Ans:
5|Page
C
Ans:
{1: 'A', 2: 'B', 3: 'C'}
Ans:
4
24 What will be the output of following program:
box = {}
jars = {}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
jars['jam'] = 4
crates['box'] = box
crates['jars'] = jars
print(len([crates]))
6|Page
Ans:
1
25 What will be the output of following program:
dict = {'c': 97, 'a': 96, 'b': 98}
for _ in sorted(dict):
print (dict[_])
Ans:
96
98
97
Ans:
False
Ans:
False
28 What will be the output of following program:
my_dict = {}
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10
7|Page
my_dict[(1,2)] = 12
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)
print(my_dict)
Ans:
30
{(1, 2, 4): 8, (4, 2, 1): 10, (1, 2): 12}
Ans:
6
8|Page
Ans:
4
Ans:
Key error
32 What will be the output of following program:
a = {(1,2):1,(2,3):2}
print(a[1,2])
Ans:
1
33 What will be the output of following program:
a={ 1:'a', 2:'b', 3:'c'}
for i,j in a.items():
print(i,j,end="#")
Ans:
1 a#2 b#3 c#
Ans:
{1: 3, 2: 1, 3: 1, 5: 2, 6: 2}
9|Page
plant={}
plant[1]='Rohit'
plant[2]='Sharma'
plant['name']='Ishant'
plant[4]='Sharma'
print (plant[2])
print (plant['name'])
print (plant[1])
print (plant)
Ans:
Sharma
Ishant
Rohit
{1: 'Rohit', 2: 'Sharma', 'name': 'Ishant', 4: 'Sharma'}
Ans:
dict_items([('Name', 'Rohit'), ('Age', 30)])
Ans:
10 | P a g e
d2 = {"x": "4", "y": "500"}
d = d1.copy()
d.update(d2)
print(d)
print(d1.keys())
print(d1)
print(d1.values())
Ans:
d1 = {"a": 500, "b": 200,"c":50,"d":150}
print(d1)
d = sorted(d1)
min=d[0]
max=d[-1]
print(min, "has min val")
print(max,"Has max val")
40 Write a Python program to multiply all the items in a dictionary
Ans:
Ans:
11 | P a g e
#print(dict.items())
res = {}
for key,value in dict.items():
if value not in res.values():
res[key] = value
print(res)
Ans:
my_dict = {}
if not bool(my_dict):
print("Dictionary is empty")
else:
print("Dictionary is not empty")
Ans:
dic = {}
dic1 = {1:10,2:20}
dic2 = {3:30,4:40}
dic3 = {5:50,6:60}
12 | P a g e
for d in (dic1, dic2, dic3):
dic.update(d)
print(dic)
45 Python Program to Add a Key-Value Pair to the Dictionary
Ans:
Ans:
d={'A':1,'B':2,'C':3}
key=input("Enter key to check:")
if key in d.keys():
print("Key is present and value of the key is:")
print(d[key])
else:
print("Key isn't present!")
47 Python Program to Generate a Dictionary that Contains Numbers (between 1
and n) in the Form (x,x*x).
Ans:
n=int(input("Enter a number:"))
d={x:x*x for x in range(1,n+1)}
print(d)
13 | P a g e
48 Python Program to Create a Dictionary with Key as First Character and Value
as Words Starting with that Character.
Ans:
test_string=input("Enter string:")
l=test_string.split()
d={}
for word in l:
if(word[0] not in d.keys()):
d[word[0]]=[]
d[word[0]].append(word)
else:
if(word not in d[word[0]]):
d[word[0]].append(word)
for k,v in d.items():
print(k,":",v)
49 Write a program in python using dictionary to print the name and salary of
employee.
Ans:
names=[]
dept=[]
salary=[]
details={'Emp Name':names,'Department':dept,'Salary':salary}
rec=int(input('How many records U want to insert'))
for i in range(rec):
n=input('Enter Emp name::')
names.append(n)
d=input('Enetr Emp Department::')
dept.append(d)
s=int(input('Enetr Emp salary::'))
salary.append(s)
14 | P a g e
for key,val in details.items():
print(key, "=>", val)
50 Write a program to count the frequency of each character in string using
dictionary.
Ans:
15 | P a g e