ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
d = {k: v for k, v in [('a', 1), ('b', 2), ('a', 3)] if v > 1}
print(d, {v: k for k, v in d.items()})- A. {'a': 3, 'b': 2} {2: 'b', 3: 'a'}
- B. {'b': 2, 'a': 1} {2: 'b', 1: 'a'}
- C. {'b': 2, 'a': 3} {2: 'b', 3: 'a'}
- D. {'b': 2} {2: 'b'}
Correct answer: C. {'b': 2, 'a': 3} {2: 'b', 3: 'a'}
The comprehension keeps pairs with a value above 1 in order, so d lists b before a. Inverting d keeps that order.
More Python Programming questions
- What is printed? python first, mid, last = [1, 2, 3, 4, 5] a, (b, c) = 1, (2, 3, 4) print(mid, last, b, c)
- What is printed? python def ack(m, n): if m == 0: return n + 1 if n == 0: return ack(m - 1, 1) return ack(m - 1, ack(m, n - 1))…
- What is printed? python d = {'b': 2, 'a': 2, 'c': 1} print(sorted(d.items(), key=lambda kv: (-kv[1], kv[0])), sorted(d, key=d.get))
- Which statement about itertools.groupby is correct?
- What is printed? python print(-1 >> 1, -8 >> 2, 1 << 70 > 1020, (-5) & 0xF)
- What is printed? python print(0 or [] or 'x', 2 and 0 and 5, None or 0)