list vs tuple vs deque vs array — which and why?

Pick by the operation you do most. list for ordered mutable data with random access; tuple when it must not change or must be a dict key; deque when you touch the front; array when you have millions of numbers; set when you only ask "is it in there?".

Overview

tuple versus list

The textbook answer is "tuples are immutable", which is true and not the point. The consequence is that a tuple is hashable (if its contents are), so it can be a dictionary key or a set member — which is why coordinates, database rows and cache keys are tuples.

The secondary signal is meaning. A list is a homogeneous sequence of unknown length; a tuple is a fixed-size record where position carries meaning. (x, y) is a point; [x, y] is two numbers.

Lists & arraysConceptualEasy

Step through it

What to watch

  • The front of a list is the trap — O(n), where a deque is O(1).
  • Hashability is the real tuple/list distinction, not immutability for its own sake.
  • An array stores values; a list stores references to them.

Say this out loud

"Tuple if it's fixed or needs to be hashable - it can be a dict key, a list can't. deque if I'm touching the front, because list.pop(0) is O(n). array or numpy for large numeric data, since a list stores pointers rather than values."

list vs tuple vs deque vs array — which and why?

When would you use a tuple instead of a list? What about deque or array?

deque versus list

collections.deque is a doubly linked list of blocks, so appendleft and popleft are O(1) where the list equivalents are O(n). Any queue, BFS frontier or "last N items" buffer should be a deque.

The trade is random access: d[5000] walks the blocks, so it is O(n). If you index into the middle, keep the list. A deque also takes maxlen, which turns it into a fixed-size ring buffer that discards the oldest entry automatically.

array and the memory question

A list holds references, so a list of a million integers is a million pointers plus a million integer objects. array.array packs the values inline in one typed block, which is several times smaller and much friendlier to the cache.

For real numeric work the answer is numpy, which adds vectorised operations on top of the same packed layout. Mentioning that a list of numbers is a list of pointers is usually the specific thing an interviewer is listening for.

Run it in Python

The five containers measured rather than described: the operation each is fast at, the memory each uses for the same thousand integers, and the two errors that pick the container for you.

containers.pyPython 3
Output

How the code works

  1. lst.insert(0, 1) versus dq.appendleft(1)The same logical operation, O(n) against O(1). Doing it in a loop is the most common accidental quadratic in Python, and it usually appears as a hand-rolled queue.
  2. dq[N // 2]Where the deque loses. It is a linked list of blocks, so indexing into the middle walks them. If you index, keep the list.
  3. sys.getsizeof(numbers) versus the arrayA list stores references; an array stores the values. That is why the array is smaller and why it can hold only one type — and it is the specific point interviewers listen for.
  4. deque(maxlen=3)A fixed-size ring buffer for free: appending past the limit drops the oldest. That is the "last N events" structure, without any bookkeeping.

Change one thing

  • Compare sys.getsizeof for a list and a tuple of the same items. The tuple is smaller because it never has to leave growth room.
  • Build a numpy array of a million floats and compare with a list. The gap is much larger than the array module's.

Where this runs

Real CPython, compiled to WebAssembly and running on your own machine — nothing is uploaded. The first run takes a few seconds while the interpreter downloads; after that it is immediate. Need more room, or want to paste your own attempt? Use the Python compiler.

Check yourself

0 of 3

Answer without scrolling back up.

  1. The most practical difference between a tuple and a list is that a tuple:

  2. You need a queue. Which container?

  3. array.array uses less memory than a list of the same integers because it:

Cheat sheet

list vs tuple vs deque vs array — which and why?

Pick by the operation you do most. list for ordered mutable data with random access; tuple when it must not change or must be a dict key; deque when you touch the front; array when you have millions of numbers; set when you only ask "is it in there?".

INTERVIEW · vizlearn.in/interview/list-versus-tuple-versus-deque.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.