ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from collections import deque
d = deque([1, 2, 3, 4, 5], maxlen=3)
d.append(6)
d.appendleft(0)
print(list(d))
  1. A. [0, 5, 6]
  2. B. [0, 4, 5]
  3. C. [0, 1, 2]
  4. D. [4, 5, 6]

Correct answer: B. [0, 4, 5]

A maxlen deque keeps only the newest items: creation keeps [3, 4, 5], append(6) drops the leftmost item to give [4, 5, 6], and appendleft(0) drops the rightmost item 6, leaving [0, 4, 5].

More Python Programming questions

Exams that ask this topic