Python: list vs dict (How To Answer): Interview Answer Guide 2027

Python: list vs dict (How To Answer): Interview Answer Guide 2027

Python: list vs dict (How To Answer): 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

Structure your answer as definition, contrast, then judgment:

  • Define both in one line each. "A list is an ordered sequence accessed by position; a dict is a key-value mapping accessed by key."
  • Contrast access and complexity. Index O(1) vs. key-hash O(1) average; value search O(n) in a list.
  • Give the decision rule. Ordered/positional → list; keyed lookup → dict.
  • Show fluency. Mention list/dict comprehensions and that dict keys must be hashable.

Sample 30-second answer: "Lists are ordered sequences you index by position; dicts map keys to values with average constant-time lookup. I pick a list for ordered data and a dict for keyed access — for example, storing users in a dict keyed by id so lookup is O(1) instead of scanning a list."

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 TikTok's interview? Our 2027 TikTok Online Hackerrank Coding Assessment Tutorials has practice questions and answers — $79 one-time, instant download.