Flatten nested dictionaries, compressing keys
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Flattening nested dictionaries is useful for logging, analytics pipelines, CSV export, and configuration normalization. The usual goal is to turn hierarchical keys into a single-level mapping like parent.child.leaf: value. The challenge is preserving enough structure to avoid collisions while still producing compact keys. You also need a predictable strategy for lists inside dictionaries and for non-string keys. A robust flattening function should be deterministic, configurable, and reversible when needed. This article covers practical patterns for compressing nested keys in Python with recursion and clear delimiter rules.
Core Sections
1. Basic recursive flattening
A standard approach traverses the dictionary recursively and joins key segments.
This is concise and works for most dictionary-only structures.
2. Handling lists in nested payloads
APIs frequently return arrays inside objects. Include list indexes in keys.
This preserves positional context and avoids data loss.
3. Prevent key collisions
Collision example: original key contains the separator (.). Use escaping, a rare separator, or tuple keys internally.
If round-tripping is required, define escaping rules explicitly and test them.
4. Compression trade-offs
Short key formats save storage but reduce readability. For observability logs, readable keys are often worth the extra bytes. For high-volume telemetry, compact delimiters and optional key maps can reduce payload size.
5. Performance and memory strategy
Recursive flattening creates new dict entries for every leaf. For very large objects, consider a generator that yields (key, value) pairs and stream into sinks rather than building one giant dictionary.
This supports memory-conscious pipelines.
Validation and production readiness
A reliable implementation should include more than a working snippet. Add a small reproducible dataset or input fixture that exercises expected behavior and edge cases, then codify it in automated tests. Include at least one “happy path,” one malformed input case, and one boundary condition so regressions are caught early. Instrument key steps with structured logs or metrics to make failures diagnosable in runtime environments, not just local development. If performance is relevant, keep a lightweight benchmark that can be rerun after refactors to ensure behavior stays within budget.
Operationally, document assumptions near the code: required library versions, environment variables, timezone/locale expectations, and failure handling strategy. For team workflows, add one integration test that mirrors real usage rather than only unit-level checks. This reduces drift between example code and production behavior. Treat these checks as part of feature completion, because most long-term issues are caused by unvalidated assumptions rather than syntax errors.
Common Pitfalls
- Flattening without a collision strategy for keys containing delimiters.
- Ignoring lists and accidentally dropping nested array values.
- Assuming all keys are strings and failing on numeric or mixed key types.
- Creating irreversible flattened forms when downstream requires reconstruction.
- Building massive intermediate dicts instead of streaming key-value pairs.
Summary
Flattening nested dictionaries is straightforward when rules are explicit: delimiter choice, list indexing behavior, and collision handling. Recursive functions provide clean implementations, while generator-based variants help with large payloads. Choose readability or compactness based on downstream needs, and add tests for edge cases like delimiter collisions and list-heavy structures. With those guardrails, flattened-key representations remain reliable and scalable.
Related reading
- Flattening a shallow list in Python
- Flip two-dimensional associative array in PHP
- foreach vs someList.ForEach
- Format y axis as percent
- Float types are not supported. Use Decimal types instead
- For i 0, why is i i equal to 0?
- Four color theorem Java implementation of U.S. map
- Freezing graph to pb in Tensorflow2

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.