ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
print({k: v for k, v in zip(['a', 'b', 'a'], [1, 2, 3])})
  1. A. {'a': 1, 'b': 2, 'a': 3}
  2. B. {'a': 1, 'b': 2}
  3. C. {'a': 3, 'b': 2}
  4. D. KeyError

Correct answer: C. {'a': 3, 'b': 2}

zip pairs up ('a',1), ('b',2), ('a',3). A dict cannot hold two entries with the same key, so the later pair for 'a' (value 3) overwrites the earlier one.

More Python Programming questions

Exams that ask this topic