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])- A. {'a': [3, 1], 'b': [2]} ('a', [3, 1])
- B. {'a': [1, 3], 'b': [2]} ('b', [2])
- C. {'a': [1, 3], 'b': [2]} ('a', [1])
- 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
- What is printed? python ops = {} for name in ['add', 'mul']: ops[name] = lambda x, y, n=name: x + y if n == 'add' else x y…
- What is printed? python print(list(enumerate('ab', start=1)), list(zip(reversed([1, 2, 3]), 'xyz')))
- What is printed? python print(hash(1) == hash(True), hash(-1) == -2)
- Which statement about dictionary iteration order in Python 3.7 and later is correct?
- What is printed? python class Q: def init(self): self.i, self.o = [], [] def push(self, x): self.i.append(x) def pop(self): if not self.o…
- What is printed? python print([y for x in [[1, 2], [3], []] for y in x if y % 2])