ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
words = ['b', 'a', 'c']
pairs = list(zip(sorted(words), range(10, 13)))
print(pairs, dict(pairs)['c'], list(zip(*pairs))[0])
  1. A. [('a', 10), ('b', 11), ('c', 12)] 12 ('c', 'b', 'a')
  2. B. [('a', 10), ('b', 11), ('c', 12)] 12 ('a', 'b', 'c')
  3. C. [('a', 10), ('b', 11), ('c', 12)] 10 ('a', 'b', 'c')
  4. D. [('b', 10), ('a', 11), ('c', 12)] 12 ('a', 'b', 'c')

Correct answer: B. [('a', 10), ('b', 11), ('c', 12)] 12 ('a', 'b', 'c')

sorted(words) is ['a', 'b', 'c'], and zip pairs each letter with 10, 11, 12. dict(pairs)['c'] is 12. Transposing the pairs with zip(*pairs) gives two tuples, and the first one is the letters.

More Python Programming questions

Exams that ask this topic