ProgrammingModerateTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import bisect a = [(1, 'x'), (4, 'y')] bisect.insort(a, (3, 'z'), key=lambda t: t[0]) print(a)
- A. [(3, 'z'), (1, 'x'), (4, 'y')]
- B. [(1, 'x'), (3, 'z'), (4, 'y')]
- C. [(1, 'x'), (4, 'y'), (3, 'z')]
- D. [(1, 'x'), (3, 'z'), (4, 'y'), (3, 'z')]
Correct answer: B. [(1, 'x'), (3, 'z'), (4, 'y')]
insort with key inserts a whole element at the position found by comparing key values. The key is not applied to the value being inserted, so you must pass the element, here the tuple (3, 'z').
More Python Programming questions
- What is printed? python import heapq h = [1, 3, 5] r = heapq.heapreplace(h, 0) print(r, sorted(h))
- What is printed? python import heapq h = [1, 3, 5] r = heapq.heappushpop(h, 0) print(r, sorted(h))
- What is printed? python import heapq h = [] try: heapq.heappush(h, (1, {'a': 1})) heapq.heappush(h, (1, {'b': 2})) except TypeError as e…
- What is printed? python import heapq print(heapq.nlargest(2, ['pear', 'fig', 'apple'], key=len))
- What is printed? python import heapq h = [] for t in [(2, 'b'), (2, 'a'), (1, 'z')]: heapq.heappush(h, t) print([heapq.heappop(h) for in…
- What is printed? python import heapq print(heapq.nsmallest(3, [3, 1, 3, 2], key=lambda x: -x))