You are on page 1of 15

Q.

Which of the following will result in an error for a given


valid dictionary D?

(i)

D+3

(ii)

D*3

(iii)

D + {3 : "3"}

(iv)

D.update ({3 : "3"})

(v)

D.update({"3" : 3})

(vi)

D.update("3": 3)

Answer =

Options (i), (ii), (iii), & (vi)


Q. The following code is giving some error. Find out the
error and correct it.

d1 = {"a" : 1, 1 : "a", [1, "a"] : "two"}

Answer =

There should not list as key. If we use tuple in place of list as a key then, it give no error.

Like this: -
 d1 = {"a" : 1, 1 : "a", (1, "a") : "two"}

.3 The following code has two dictionaries with tuples


as keys. While one of these dictionaries being
successfully created, the other is giving some error.
Find out which dictionary will be successfully and
which one will give error and correct it:

dict1 = {(1, 2) : [1, 2], (3, 4) : [3, 4]}

dict2 = {([1], [2]) : [1, 2] : ([3], [4]) : [3, 4]}

Answer =

Dictionary ‘dict1’ will successfully run and dictionary ‘dict2’ will give error.
It can be corrected by removing list from tuple which is as a key.

So, 

dict2 =  {(1, 2) : [1, 2], (3, 4) : [3, 4]}


4. Q. Nesting of dictionary allows you to store a
dictionary inside another dictionary. Then why is
following code raising error? What can you do to
correct it?

d1 = {1 : 10, 2 : 20, 3: 30}


d2 = {1 : 40, 2 : 50, 3 : 60}

d3 = {1 : 70, 2 : 80, 3 : 90}

d4 = {d1 : "a", d2 : "b", d3 : "c"}

Answer =

Because here d1, d2 and d3 are as keys of dictionary d4, but we know that keys should be
immutable data type. But here we provide dictionary as key which is mutable data type.

It can be corrected by replacing key and value of dictionary d4 like that:

d4 = {"a" : d1 , "b" : d2, "c" : d3}

5Q.Why is following code not giving correct output


even when 25 is a member of the dictionary?

dic1 = {'age' : 25, 'name' : "xyz", 'salary' : 23450.5}

val = dic1['age']

if val in dic1 :

    print("This is member of the dictionary")

else :

    print("This is not a member of the dictionary")

Answer =

Because ‘in’ condition check only that key is present or not in dictionary. But in given
program, value is passed in ‘in’ condition.

6Q. What is the output produced by above code:

d1 = {5 : [6, 7, 8],"a" : (1, 2, 3)}


print (d1.keys())
print(d1.values())
Answer =

Output: -

dict_keys([5, 'a'])
dict_values([[6, 7, 8], (1, 2, 3)])
>>>

7. Q.
Consider the following code and then answer the
questions that follow:
myDict = {'a' : 27, 'b' : 43, 'c' : 25, 'd' : 30}
valA = ''
for i in myDict :
    if i > valA :
        valA = i
        valB = myDict[i]
print (valA)    # Line 1
print (valB)    # Line 2
print (30 in myDict)    # Line 3
myLst = list((myDict.items()))
myLst.sort()    # Line 4
print (myLst[-1])    # Line 5

Answer =

(i) d
(ii) 30
(iii) False
(iv) error
(v) error

Here Line 4 produce Error because myLst is not list.

myLst = dict_items([('a', 27), ('b', 43), ('c', 25), ('d', 30)])

its data type is not List.

So, Correct code :-

myDict = {'a' : 27, 'b' : 43, 'c' : 25, 'd' : 30}


valA = ''
for i in myDict :
if i > valA :
valA = i
valB = myDict[i]
print (valA)# Line 1
print (valB)# Line 2
print (30 in myDict)# Line 3
myLst = ((myDict.items()))
myLst.sort()# Line 4
print (myLst[-1])# Line 5"""

Now Output is :-

d
30
False
('d', 30)
>>> 

 So, Correct answer of (iv) and (v) is :-

(iv) ('d', 30)

(v) myLst = [('a', 27), ('b', 43), ('c', 25), ('d', 30)]

Data Type = List

8Q. What will be the output produced by following code?

d1 = {5 : "number", "a" : "string", (1, 2) : 'tuple'}

print ("Dictionary contents")

for x in d1.keys() : # Iterate on a key list

    print (x, ':', d1[x], end = ' ')

    print (d1[x] * 3)

    print ()
Answer =

Output: -

Dictionary contents
5 : number numbernumbernumber
a : string stringstringstring
(1, 2) : tuple tupletupletuple
>>>
9Q. Predict the output:

(i)

d = dict ()

d ['left'] = '<'

d ['right'] = '>'

print ('{left} and {right} or {right} and {left}')

(ii)

d = dict ()

d ['left'] = '<'

d ['right' ] = '>'

d ['end'] = ' '

print(a ["left"] and d ['right'] or d ['right'] and d ['left'])

print (d ['left'] and d ['right'] or d ['right'] and d ['left'] )

print( (d['left'] and d [ 'right'] or d ['right'] and d ['left'] ) and d ['end'])

print ("end")
(iii)

text = "abracadabraaabbccrr"

counts = {}

ct = 0

lst = []

for word in text :

    if word not in lst :

        lst . append(word)

        counts[word] = 0

    ct = ct + 1

    counts [word] = counts [word] + 1

    print (counts)

    print (lst)

(iv)

list1 = [2, 3, 3, 5, 3, 2, 5, 1, 1]

counts = {}

ct = 0

lst = []

for num in list1 :

    if num not in lst :

        lst . append(num)

        counts[num] = 0

    ct = ct + 1

    counts[num] = counts[num] + 1

    print (counts)


    for key in counts.keys() :

        counts[key] = key * counts [key]

    print (counts)

Answer =

Output: -

(i)

{left} and {right} or {right} and {left}


>>>

(ii)

>
>
end
>>>

(iii)

{'a': 1}
['a']
{'a': 1, 'b': 1}
['a', 'b']
{'a': 1, 'b': 1, 'r': 1}
['a', 'b', 'r']
{'a': 2, 'b': 1, 'r': 1}
['a', 'b', 'r']
{'a': 2, 'b': 1, 'r': 1, 'c': 1}
['a', 'b', 'r', 'c']
{'a': 3, 'b': 1, 'r': 1, 'c': 1}
['a', 'b', 'r', 'c']
{'a': 3, 'b': 1, 'r': 1, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 4, 'b': 1, 'r': 1, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 4, 'b': 2, 'r': 1, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 4, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 6, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 3, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 2, 'c': 2, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 2, 'c': 3, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 3, 'c': 3, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 4, 'c': 3, 'd': 1}
['a', 'b', 'r', 'c', 'd']
>>>

(iv)

{2: 1}
{2: 2}
{2: 2, 3: 1}
{2: 4, 3: 3}
{2: 4, 3: 4}
{2: 8, 3: 12}
{2: 8, 3: 12, 5: 1}
{2: 16, 3: 36, 5: 5}
{2: 16, 3: 37, 5: 5}
{2: 32, 3: 111, 5: 25}
{2: 33, 3: 111, 5: 25}
{2: 66, 3: 333, 5: 125}
{2: 66, 3: 333, 5: 126}
{2: 132, 3: 999, 5: 630}
{2: 132, 3: 999, 5: 630, 1: 1}
{2: 264, 3: 2997, 5: 3150, 1: 1}
{2: 264, 3: 2997, 5: 3150, 1: 2}
{2: 528, 3: 8991, 5: 15750, 1: 2}
>>>

10Q.Create a dictionary 'ODD' of odd numbers between


1 and 10, where the key is the decimal number and the
value is the corresponding number in words. Perform
the following operations on this dictionary:

(a) Display the keys

(b) Display the values

(c) Display the items

(d) Find the length of the dictionary

(e) Check if 7 is present or not

(f) Check if 2 is present or not

(g) Retrieve the value corresponding to the key 9

(h) Delete the item from the dictionary corresponding to the key 9

Answer =

Let the dictionary name is ‘dic’.

(a) dic.keys()
(b) dic.values()
(c) dic.items()
(d) len( dic )
(e) 7.0 in dic
(f)  2.0 in dic
(g) dic[ 9.0 ]
(h) del dic [ 9.0 ]

11Q. Find the errors:

(a)

text = "abracadbra"

counts = {}

for word in text :

    counts[word] = counts [word] + 1


(b)

(b)

my_dict = {}

my_dict[1, 2, 4] = 8

my_dict[[4, 2, 1]] = 10

print (my_dict)

Answer =

(a) Error in line 4.


(b) Error in line 2 & 3 because here list is as key which is not acceptable.

12Q. Predict the output:

(a)

fruit = {}

L1 = ['Apple', 'banana', 'apple']

for index in L1 :

    if index in fruit :

        fruit [index] += 1

    else :

        fruit [index] = 1

print (len(fruit))

print (fruit)

(b)

arr = {}

arr [1] = 1
arr ['1'] = 2

arr[1] += 1

sum = 0

for k in arr :

    sum += arr[1]

print (sum)

Answer =

Output: -

(a)

3
{'Apple': 1, 'banana': 1, 'apple': 1}
>>>

(b)

4
>>>

13Q. Predict the output:

(a)

a = {(1, 2) : 1, (2, 3) : 2}

print(a[1, 2])

(b)

a = {'a' : 1, 'b' : 2, 'c' : 3}

print (a['a', 'b'])

Answer =
Output: -

(a)

1
>>>

(b)
Error : (

14Q.Find the error/output. Consider below given two sets of


codes. Which one will produce an error? Also, predict the
output produced by the correct code.

(a)

box = {}

jars = {'Jam' : 4}

crates = {}

box['biscuit'] = 1

box['cake'] = 3

crates['box'] = box

crates['jars'] = jars

print (len(crates[box]))

(b)

box = {}

jars = {'Jam' : 4}

crates = {}
box['biscuit'] = 1

box['cake'] = 3

crates['box'] = box

crates['jars'] = jars

print (len(crates['box']))

Answer =

Output: -

(a) Error in line 8.

(b)

2
>>>
15 Q. Predict the output:

dct = {}

dct[1] = 1

dct['1'] = 2

dct[1.0] = 4

sum = 0

for k in dct:

    print(k, sum)

    sum += dct[k]


print (sum)

Answer =

10
14
6

16 Q.Fill in the blanks of the following code so that the values


and keys of dictionary d are inverted to create dictionary fd.

d = {'a':1, 'b':2, 'c':3}

print(d)

fd = { }

for key, value in d. _____() :

    fd [____] = [____]

print(fd)

Answer =

d = {'a':1, 'b':2, 'c':3}


print(d)
fd = { }
for key, value in d.items() :
fd [value] = [key]
print(fd)

You might also like