Back to questions
JuniorTheoryCommon
What does the program print?
from collections import deque
stack = [1, 2, 3]
queue = deque([1, 2, 3])
print(stack.pop(), queue.popleft())3 1
- Expecting
pop()to take the first item, like a queue: without an argumentlist.popremoves the last one, which is how a list works as a stack. - Expecting
popleftto take from the right end:leftis the start of thedeque, so it returns1, the item added first.
- Why is
list.pop(0)slow on a long list whiledeque.popleft()is not? - What does
queue.pop()return on the samedeque?