ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
words = ['pear', 'fig', 'banana']
index = {w: i for i, w in enumerate(words)}
print(index['fig'], index.get('kiwi', -1), sorted(index, key=index.get)[0])
  1. A. 0 -1 pear
  2. B. 1 None pear
  3. C. 1 -1 pear
  4. D. 1 -1 fig

Correct answer: C. 1 -1 pear

enumerate gives 'pear' index 0, 'fig' index 1 and 'banana' index 2. get with a default returns -1 for the missing key. sorted by the index value puts pear first.

More Python Programming questions

Exams that ask this topic