ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
d = {"x": [1, 2], "y": [3]}
flat = [v for vs in d.values() for v in vs]
lengths = {k: len(v) for k, v in d.items()}
print(flat, lengths, max(lengths, key=lengths.get))
  1. A. [1, 2, 3] {'x': 2, 'y': 1} [1, 2]
  2. B. [1, 2, 3] {'x': 2, 'y': 1} 2
  3. C. [1, 2, 3] {'x': 2, 'y': 1} y
  4. D. [1, 2, 3] {'x': 2, 'y': 1} x

Correct answer: D. [1, 2, 3] {'x': 2, 'y': 1} x

In a nested comprehension the for clauses read left to right as nested loops, flattening the lists. max over a dict iterates keys and, with key=lengths.get, picks the key with the largest value.

More Python Programming questions

Exams that ask this topic