Python: list vs dict (Explained): Interview Answer Guide 2027

Python: list vs dict (Explained): Interview Answer Guide 2027

Python: list vs dict (Explained): Interview Answer Guide 2027

A Python list is an ordered, mutable sequence accessed by position — `items[0]` — while a dict is an unordered (insertion-ordered) key-value mapping accessed by key — `user['name']`. This python basics interview question is really about choosing well: lists for ordered collections and iteration, dicts for fast lookups by a unique key, with average O(1) access on both.

What the Python Basics Interview Question Tests

  • Whether you know the core differences: ordered sequence vs. key-value mapping, index vs. key access.
  • Whether you can justify a choice — 'I would use a dict here because lookups by id must be fast.'
  • Whether you know the complexities: O(1) average access for both, O(n) search in a list.

How to Answer the Python Basics Interview Question

Explain the two structures by contrasting four dimensions:

  • Shape. A list is an ordered sequence: `[10, 20, 30]`. A dict is a key→value mapping: `{'a': 10, 'b': 20}`.
  • Access. Lists by position — `items[0]`; dicts by key — `prices['apple']`. Both average O(1).
  • Search. Finding a value in a list scans it: O(n). Checking a key in a dict hashes it: O(1) average.
  • Use case. Lists: queues of tasks, rows of data, anything ordered. Dicts: caches, indexes, JSON-like records.

Close with the decision rule: "Order and position → list. Lookup by identity → dict."

Sample answer: "A list is an ordered mutable sequence accessed by index; a dict maps hashable keys to values with average O(1) lookup. I use lists for sequences I iterate in order and dicts when I need fast lookup by key — like indexing users by id."

Common Mistakes With the Python Basics Interview Question

  • Saying dicts are 'unordered' without nuance — since Python 3.7 they preserve insertion order, but you still must not rely on position.
  • Using a list for membership tests in a loop: `x in my_list` is O(n) each time; a set or dict is O(1).
  • Using mutable default arguments or dict keys that are mutable — dict keys must be hashable.

'List vs dict' sounds trivial, which is why it is a perfect filter: weak candidates recite syntax, strong candidates talk about access patterns and complexity. Always answer the 'when would you use each' follow-up before it is asked.

Keep Reading

FAQ

When should I use a list vs a dict?

Use a list for ordered data you iterate or index by position. Use a dict when you look things up by a unique key, like user records by id.

What is the time complexity of lookup in each?

Indexing a list is O(1); searching a list by value is O(n). Dict lookup by key is O(1) on average thanks to hashing.

Can a dict have duplicate keys?

No — assigning to an existing key overwrites the value. If you need multiples, map each key to a list.

What is the difference between a dict and a set?

A set stores only unique keys with no values, optimized for membership tests and deduplication. A dict maps each key to a value.

Preparing for Telstra's interview? Our 2027 Telstra Online Assessment | Video Interview Tutorials has practice questions and answers — $79 one-time, instant download.