Predict the output
What does this print?
from collections import deque
items = [1, 2, 3]
stack = list(items)
queue = deque(items)
print(stack.pop(), queue.popleft())Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 3 1
Right: the stack hands back the newest item, the queue the oldest.
The same three items leave in opposite orders, which is the whole difference between the two structures.
Why not the others?
- B 1 1. list.pop() takes from the end.
- C 3 3. popleft takes from the front.