Dictionaries Output#

  • Find the output of the following code.

  • Please don’t run the code before giving your answer.     

Question-1#

dict_exp = {'table':1, 'chair':2, 'washer':3}

for i in dict_exp.keys():
  print(i[-2])

Solution

l
i
e

Question-2#

dict_exp = {'table':[1,5,9], 'chair':[10,20,30], 'washer':[2,7,4]}
total = 0

for i in dict_exp.values():
  total += i[1]
    
print(total)

Solution

32

Question-3#

dict_exp = {'table':[1,5,9], 'chair':[4,6,8], 'washer':[2,7,4]}
total = 0

for i in dict_exp.values():
  for j in i:
    total += i[1]
      
print(total)

Solution

54

Question-4#

dict_exp = {'table':[1,5,9], 'chair':[4,6,8], 'washer':[2,7,4]}
total = 0

for i in dict_exp.values():
  for j in i:
    total += j
      
print(total)

Solution

46

Question-5#

stock_dict = {'day-1':{'High': 70, 'Low':62, 'Close':65},
              'day-2':{'High': 68, 'Low':60, 'Close':63},
              'day-3':{'High': 71, 'Low':65, 'Close':67},
              'day-4':{'High': 70, 'Low':62, 'Close':65},
              'day-5':{'High': 73, 'Low':65, 'Close':70},
              'day-6':{'High': 75, 'Low':69, 'Close':73}
              }

for i in [1,3]:
  print(stock_dict['day-'+str(i)]['Low'])

Solution

62
65

Question-6#

stock_dict = {'day-1':{'High': 70, 'Low':62, 'Close':65},
              'day-2':{'High': 68, 'Low':60, 'Close':63},
              'day-3':{'High': 71, 'Low':65, 'Close':67},
              'day-4':{'High': 70, 'Low':62, 'Close':65},
              'day-5':{'High': 73, 'Low':65, 'Close':70},
              'day-6':{'High': 75, 'Low':69, 'Close':73}
              }
total = 0

for i,j in stock_dict.items():
  total += int(i[-1])

print(total)

Solution

21

Question-7#

stock_dict = {'day-1':{'High': 70, 'Low':62, 'Close':65},
              'day-2':{'High': 68, 'Low':60, 'Close':63},
              'day-3':{'High': 71, 'Low':65, 'Close':67},
              'day-4':{'High': 70, 'Low':62, 'Close':65},
              'day-5':{'High': 73, 'Low':65, 'Close':70},
              'day-6':{'High': 75, 'Low':69, 'Close':73}
              }
total = 0

for i,j in stock_dict.items():
  total += j['High']//10

print(total)

Solution

41

Question-8#

my_dict = {200:'NY', 100:'NJ', 400:'TX', 300:'CA'}

for i in my_dict.keys():
  if i>188:
    print(my_dict[i][1])

Solution

Y
X
A

Question-9#

mydict = {'A':(10,100), 'B':(20, 200), 'C':(30,300)}

for i,j in mydict.values():
  print(i,j)

Solution

10 100
20 200
30 300