Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Managing Order of Processing

Stacks, queues, and deques model how work moves through a system

Sets focused on uniqueness. Now the question changes: when several tasks are waiting, which one should be processed next? These structures help you express that order directly.

Core Explanation

Stacks and queues are both ordered structures, but they remove elements differently.

  • a stack is last-in, first-out

  • a queue is first-in, first-out

  • a deque supports efficient operations from both ends

Continuity from sets

Sets helped answer “is this value present?” Stacks and queues answer “which value should be processed next?”

Worked Example: browser history versus a task line

The first code example shows the main contrast clearly: a stack behaves like browser history, where the most recent page is the first one you leave. A queue behaves like a task line, where the oldest waiting task should be processed first.

Exercises

  1. Describe one real-world example of a stack.

  2. Describe one real-world example of a queue.

  3. Explain why a deque can be useful in algorithm design.

  4. Give one business or engineering workflow where processing order matters more than uniqueness.

Hint

If the newest item should be handled first, think stack. If the oldest waiting item should be handled first, think queue.

Quick Summary
  • stacks process the most recent item first

  • queues process the oldest item first

  • deques support flexible insert and remove operations at both ends

  • the key design question is which item should leave next

Practice Lab

Expected output
report
['invoice']
Expected output
task1
['task2', 'task3']

Stack, Queue, and Deque Operations

Use these smaller examples to practice one operation at a time. The goal is to notice how the structure changes after each insert or removal.

Stack push with append()

Use append() on a list to place a new item on top of the stack.

Stack pop with pop()

Use pop() to remove the most recent stack item.

Queue enqueue with append()

Use append() on a deque to add a new item at the back of the queue.

Queue dequeue with popleft()

Use popleft() to remove the oldest queued item.

Deque add to the left with appendleft()

Use appendleft() when a deque needs a fast insert at the front.

Deque remove from the right with pop()

Use pop() on a deque to remove the last item efficiently.

Guided Practice

Which behavior matches a stack?

First in, first outThat describes a queue.
Last in, first outCorrect. Stacks use LIFO order.
Sorted by keyA stack does not sort items automatically.
Unique values onlyThat is not the defining stack rule.

Which operation removes the oldest item from a queue implemented with `deque`?

append()`append()` adds an item.
popleft()Correct. `popleft()` removes the oldest queued item.
sort()`deque` is not used that way here.
reverse()That would not remove the oldest item.

Key Takeaway

Stacks, queues, and deques are about control over processing order. They become useful when the meaning of “next” matters more than lookup, uniqueness, or fixed record structure.

Deeper Python Patterns: Lists as Stacks, deque as Queue

Adapted for this course from the Python Tutorial data structures chapter.

This section extends the core processing-order idea into patterns you will use often in real Python code:

  • using a list as a LIFO stack

  • seeing why pop(0) on a list is possible but not ideal for queues

  • using collections.deque for efficient FIFO behavior

How to read this section

Focus on the cost and the meaning of each operation: where does the new item enter, and which item leaves next?

# Lists as stacks (LIFO)
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
print('After pushes:', stack)
print('pop ->', stack.pop())
print('pop ->', stack.pop())
print('Remaining stack:', stack)

# Lists as queues are possible but not efficient for left pops
queue_like_list = ['eric', 'john', 'michael']
queue_like_list.append('terry')
queue_like_list.append('graham')
print('List queue before pop(0):', queue_like_list)
print('list pop(0) ->', queue_like_list.pop(0))
print('List queue after pop(0):', queue_like_list)

# Efficient FIFO queue with deque
from collections import deque
queue = deque(['eric', 'john', 'michael'])
queue.append('terry')
queue.append('graham')
print('Deque queue before popleft:', queue)
print('popleft ->', queue.popleft())
print('popleft ->', queue.popleft())
print('Deque queue after popleft:', queue)