Python-Quizzes | Python Dictionary Quiz | Question 1

Question 1:Find the output of the following program:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


(A) {0: 1, 7: 0, 1: 1, 8: 0}
(B) {1: 1, 7: 2, 0: 1, 8: 1}
(C) {0: 0, 7: 0, 1: 1, 8: 1}
(D) KeyError


Answer: (C)

Explanation: enumerate() will return a tuple, the loop will have x = (0, 0), (1, 1). Thus D[0] = 0, D[1] = 1, D[0 + 7] = D[7] = 0 and D[1 + 7] = D[8] = 1.
Note: Dictionary is unordered, so the sequence of the key-value pair may differ in each output.

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.