ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
g = {'a': [3, 1], 'b': [2]}
for v in g.values():
    v.sort()
print(g, sorted(g.items(), key=lambda kv: len(kv[1]), reverse=True)[0])
  1. A. {'a': [3, 1], 'b': [2]} ('a', [3, 1])
  2. B. {'a': [1, 3], 'b': [2]} ('b', [2])
  3. C. {'a': [1, 3], 'b': [2]} ('a', [1])
  4. D. {'a': [1, 3], 'b': [2]} ('a', [1, 3])

Correct answer: D. {'a': [1, 3], 'b': [2]} ('a', [1, 3])

Sorting each list in place changes the dict values. Sorting by length in reverse puts the longer list first, so a comes first.

More Python Programming questions

Exams that ask this topic