Dictionaries

Python Dictionary Interview Questions

Dictionaries are how you count and group by key in Python. Almost every question phrased as "how many X per Y" turns out to be a dictionary question.

What dictionary questions ask you to do

Across the questions here, these are the shapes that come up:

  • Count how often each value appears, and take the top few
  • Group rows into lists by a key
  • Turn a mapping around so the values become the keys
  • Merge two mappings with a rule for clashes
  • Remember a result so it is not worked out twice

Common mistakes with dictionaries

Asking for a key that is not there raises an error, which is why first attempts end up full of checks for whether a key exists yet. Asking with a default value, or using defaultdict or Counter, removes every one of those checks.

Questions to practice

Roles this comes up in

Companies these come from

Other topics