Flatten and Unflatten Nested Dicts

easy
Unit 4 · Dicts0/3 to milestone

Environment variables, CSV exports and metrics systems need flat keys like db.host, while code wants nested dicts.

Implement:

  • flatten(d, sep="."): {"db": {"host": "x", "port": 5432}} → {"db.host": "x", "db.port": 5432}. Lists are kept as values (not flattened). An empty nested dict becomes {key: {}}.
  • unflatten(flat, sep="."): the inverse. Raise ValueError on conflicts such as {"a": 1, "a.b": 2}.

unflatten(flatten(d)) == d for dicts whose keys don't contain sep.

#dicts#recursion#config2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Flatten and Unflatten Nested Dicts · TechGradify