← Course home

Foundations · Lesson 5 of 52

Arrays and Python sequence costs

Choose list or deque operations based on indexing, endpoints, movement and amortized cost.

Full lessonAbout 9 min

You will learn to choose list or deque from the operation pattern and distinguish amortized append from front insertion.

Recommended first: Cost models and assumptions.

On this page 6 sections

The idea

One pop(0) on 4 items slides 3 references.

A Python list is a dynamic array of references. Indexing list[i] is Θ(1) because the address is base + i times the reference size. append and pop() at the end are cheap on average, which the next lesson counts. insert(0, x) and pop(0) move every later reference, so one call is Θ(n). A collections.deque stores blocks that can grow at either end, so append and popleft are Θ(1). Use a list when you need random access. Use a deque when both ends change and you do not need list[i] in the inner loop.

A worked trace

StepStateWhat changed
Startmoved = 0Front removal has slid 0 references toward index 0.
Slidemoved = 1Front removal has slid 1 references toward index 0.
Slidemoved = 2Front removal has slid 2 references toward index 0.

Why it is correct

The shift cost follows from the layout. If item i + 1 must occupy slot i, every index from 0 through n - 2 is written once. That is n - 1 writes, which is Θ(n). Nothing in CPython's list makes the front special. The overallocation that makes append cheap sits at the end, past the current length.

A common mistake

The cheap end of a Python list is the last index, not index 0.

What it costs

n front removals on a list copy about n + (n - 1) + ... + 1 references, which is Θ(n²). The same n removals on a deque are Θ(n) total. Random access on the deque is not the list's Θ(1) index. The choice is which operations the algorithm actually performs. The list block uses Θ(n) extra memory for n references.

A worked check is n = 1000. One pop(0) moves about 999 references. Doing that once per item moves about half a million references. A deque draining the same list moves about 1000 nodes. The asymptotic symbols summarize that count. Indexing stays Θ(1) on the list either way. A later binary search needs a random-access sequence, not a linked chain.

Run it

python
from bitsandbytes.library_costs import list_pop_front_cost_demoprint(list_pop_front_cost_demo(1000))# prints: 999 

Practice and next step

Open Dynamic-array growth next. Dynamic-array growth explains why append at that cheap end stays cheap on average.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.library_costs

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Remove the front of a list

Three items sit in a contiguous array. Removing index 0 moves the other two.

moved
0

State

  1. 00chosen

Front removal has slid 0 references toward index 0.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1You drain 1000 items with pop from the front. What dominates?
2You drain 4 items with pop from the front. How many reference moves is that?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.