Predict the output
What does this print?
inbox, outbox = [1, 2, 3], []
while inbox:
outbox.append(inbox.pop())
print(outbox, outbox[-1])Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [3, 2, 1] 1
Right: moving reverses the order, so the oldest item, 1, is on top.
One reversal puts the oldest item on top of the outbox, which is what a queue needs to hand out next.
Why not the others?
- B [1, 2, 3] 3. Popping from the inbox takes 3 first, so the order flips.
- C [3, 2, 1] 3. The top of a list-based stack is its last item, which is 1.