Open In App

# Output of python program | Set 14 (Dictionary)

Prerequisite: Dictionary
Note: Output of all these programs is tested on Python3
1) What is the output of the following program?

## PYTHON3

 `D ``=` `dict``()``for` `x ``in` `enumerate``(``range``(``2``)):``    ``D[x[``0``]] ``=` `x[``1``]``    ``D[x[``1``]``+``7``] ``=` `x[``0``]``print``(D)`

a) KeyError
b) {0: 1, 7: 0, 1: 1, 8: 0}
c) {0: 0, 7: 0, 1: 1, 8: 1}
d) {1: 1, 7: 2, 0: 1, 8: 1}
Ans. (c)
Explanation: enumerate() will return a tuple, the loop will have x = (0, 0), (1, 1). Thus D = 0, D = 1, D[0 + 7] = D = 0 and D[1 + 7] = D = 1.
Note: Dictionary is unordered, so the sequence of the key-value pair may differ in each output.
2) What is the output of the following program?

## PYTHON3

 `D ``=` `{``1` `: ``1``, ``2` `: ``'2'``, ``'1'` `: ``2``, ``'2'` `: ``3``}``D[``'1'``] ``=` `2``print``(D[D[D[``str``(D[``1``])]]])`

a) 2
b) 3
c) ‘2’
d) KeyError
Ans. (b)
Explanation: Simple key-value pair is used recursively, D = 1, str(1) = ‘1’. So, D[str(D)] = D[‘1’] = 2, D = ‘2’ and D[‘2’] = 3.
3) What is the output of the following program?

## PYTHON3

 `D ``=` `{``1` `: {``'A'` `: {``1` `: ``"A"``}, ``2` `: ``"B"``}, ``3` `:``"C"``, ``'B'` `: ``"D"``, ``"D"``: ``'E'``}``print``(D[D[D[``1``][``2``]]], end ``=` `" "``)``print``(D[D[``1``][``"A"``][``2``]])`

a) D C
b) E B
c) D B
d) E KeyError
Ans. (d)
Explanation: Key-Value Indexing is used in the example above. D = {‘A’ : {1 : “A”}, 2 : “B”}, D = “B”, D[D] = D[“B”] = “D” and D[“D”] = “E”. D = {‘A’ : {1 : “A”}, 2 : “B”}, D[“A”] = {1 : “A”} and D[“A”] doesn’t exists, thus KeyError.
4) What is the output of the following program?

## PYTHON3

 `D ``=` `dict``()``for` `i ``in` `range` `(``3``):``    ``for` `j ``in` `range``(``2``):``        ``D[i] ``=` `j``print``(D)`

a) {0: 0, 1: 0, 2: 0}
b) {0: 1, 1: 1, 2: 1}
c) {0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}
d) TypeError: Immutable object
Ans. (b)
Explanation: 1st loop will give 3 values to i 0, 1 and 2. In the empty dictionary, values are added and overwritten in j loop, for eg. D =  becomes D = 1, due to overwriting.
5) Which of the options below could possibly be the output of the following program?

## PYTHON3

 `D ``=` `{``1` `: [``1``, ``2``, ``3``], ``2``: (``4``, ``6``, ``8``)}``D[``1``].append(``4``)``print``(D[``1``], end ``=` `" "``)``L ``=` `[D[``2``]]``L.append(``10``)``D[``2``] ``=` `tuple``(L)``print``(D[``2``])`

a) [1, 2, 3, 4] [4, 6, 8, 10]
b) [1, 2, 3, 4] ((4, 6, 8), 10)
c) ‘[1, 2, 3, 4] TypeError: tuples are immutable
d) [1, 2, 3, 4] (4, 6, 8, 10)
Ans. (b)
Explanation: In the first part key-value indexing is used and 4 is appended into the list. As tuples are immutable, in the second part the tuple is converted into a list, and value 10 is added finally to the new list ‘L’ then converted back to a tuple.
This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.