What is the output of the following snippet?

What is the output of the following snippet?

dct = {}

dct[‘1’] = (1, 2)

dct[‘2’] = (2, 1)

for x in dct.keys():

print(dct[x][1], end=”)
A . 21
B. (2,1)
C. (1,2)
D. 12

Answer: A

Explanation:

Topics: dictionary tuple indexing for dict.keys() print()

Try it yourself:

dct = {}

dct[‘1’] = (1, 2)

dct[‘2’] = (2, 1)

print(dct) # {‘1’: (1, 2), ‘2’: (2, 1)}

for x in dct.keys():

print(dct[x][1], end=”) # 21

print()

print(dct[‘1’][1]) # 2

print(dct[‘2’][1]) # 1

dct.keys() are the keys ‘1’ and ‘2’

dct[‘1’][1] is 2 and

dct[‘2’][1] is 1

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments