ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
data = [("b", 2), ("a", 2), ("c", 1), ("a", 1)]
print(sorted(data, key=lambda t: t[1]), sorted(data)[0])- A. [('a', 1), ('c', 1), ('a', 2), ('b', 2)] ('a', 1)
- B. [('c', 1), ('a', 1), ('b', 2), ('a', 2)] ('a', 2)
- C. [('a', 1), ('c', 1), ('b', 2), ('a', 2)] ('a', 1)
- D. [('c', 1), ('a', 1), ('b', 2), ('a', 2)] ('a', 1)
Correct answer: D. [('c', 1), ('a', 1), ('b', 2), ('a', 2)] ('a', 1)
Python's sort is stable: items with equal keys keep their original relative order, so ('c', 1) stays before ('a', 1). Sorting tuples compares element by element, making ('a', 1) smallest.
More Python Programming questions
- What is printed? python def f(a, /, b, , c): return a + b + c for call in ["f(1, 2, c=3)", "f(1, b=2, c=3)", "f(a=1, b=2, c=3)", "f(1, 2…
- What is printed? python def f(a, kw): return a, kw print(f(1, {"b": 2}), f({"a": 3, "kw": 4}), end=" ") try: f(1, {"a": 2}) except…
- What is printed? python def f(x, , y=2, kw): return x y + len(kw) opts = {"y": 3, "z": 0} print(f(2, opts), f(2, z=1, w=1))
- What is printed? python log = [] def v(x): log.append(x) return x def f(a, k): return a, k f(v(1), [v(2)], k=v(3), {"m": v(4)}) print(log)
- What is printed? python def outer(fn): print("deco", end=" ") return fn @outer def g(): print("g", end=" ") print("start", end=" ") g(); g()
- What is printed? python def a(fn): def w(): return "a(" + fn() + ")" return w def b(fn): def w(): return "b(" + fn() + ")" return w @a @b…