Python practice

Python dictionary practice questions

16 problems, ordered from easiest. They run in your browser, and 7 are free.

Start with First Occurrence Map

The idea in a minute

A dictionary stores values under keys, so you can look something up by name instead of by position. It is the tool for counting, grouping and quick lookups.

Most problems here follow one pattern: walk through the data once, and update a dictionary as you go.

Example: Group names by city

people = [("Asha", "Pune"), ("Ravi", "Delhi"), ("Meera", "Pune")]
by_city = {}
for name, city in people:
    by_city.setdefault(city, []).append(name)
print(by_city)

What it prints

{'Pune': ['Asha', 'Meera'], 'Delhi': ['Ravi']}

What to watch for

  • Use get(key, default) so a missing key does not raise an error.
  • Keys must be unchangeable, so a list cannot be a key but a tuple can.
  • Looping over a dictionary gives its keys. Use .items() for keys and values.

Dictionaries problems

  1. First Occurrence Mapbeginner · Free
  2. Word Frequency Counterbeginner · Free
  3. Tasks that are ready to runbeginner · Free
  4. Quality gate for a batchbeginner · Free
  5. Count events per minutebeginner · Free
  6. Shared tags between two campaignsbeginner · Free
  7. Counter for warehouse zone visitsbeginner · Free
  8. Daily per-user event countsintermediate · Locked
  9. Latest valid location per driverintermediate · Locked
  10. Group-by on CSV text without pandasintermediate · Locked
  11. Hash join two lists of dictsintermediate · Locked
  12. Word frequency ignoring stop wordsintermediate · Locked
  13. Group orders by customer and statusintermediate · Locked
  14. Merge two JSON-like config dicts with overridesintermediate · Locked
  15. Validate a nested order schemaintermediate · Locked
  16. Diff 2 versions of a product catalogadvanced · Locked

Want a guided order across every topic? Open the practice sheets or the Python hub.