Given a nested dictionary that represents hierarchical data, flatten it into a list of records suitable for a UI table or report.
Each record is a dictionary with two fields: "key", the full dot-separated path from the root to a leaf, and "value", the leaf value at that path.
Lists inside the dictionary are treated as dictionaries whose keys are the element indices. For example, if data["items"] is [10, 20], the flattened keys are "items.0" and "items.1". Non-empty dictionaries and non-empty lists do not appear as records of their own. An empty dictionary or an empty list is a leaf, and its record holds that empty container as the value.
Return the records sorted lexicographically by key path.
Function Signature
def flatten_dict(data: dict[str, Any]) -> list[dict[str, Any]]:
Rules
-
The examples are written as JSON:
true
,
false
and
null
are Python's
True
,
False
and
None
.
-
A path joins the keys and list indices (written in decimal) from the top level down with
.
. The path of a top-level leaf is just its key.
-
Sort with plain string comparison on the whole path. Indices are compared as text, so
"items.10"
comes before
"items.2"
. The character
.
sorts before digits,
_
and lowercase letters, so
"a.x"
comes before
"a_b"
.
-
Keys never contain
.
, so every leaf has a distinct path and the order is fully determined.
Constraints
-
The input has at most 1000 keys in total across all levels.
-
Nesting depth is at most 10.
-
Keys are non-empty strings made of lowercase letters, digits and
_
.
-
Values other than dictionaries and lists are strings, integers, booleans or
null
.
-
The input dictionary is non-empty.
Examples
Input: data = {"user": {"name": "Alice", "age": 30, "address": {"city": "Seattle", "zip": "98101"}}, "active": true}
Output: [
{"key": "active", "value": true},
{"key": "user.address.city", "value": "Seattle"},
{"key": "user.address.zip", "value": "98101"},
{"key": "user.age", "value": 30},
{"key": "user.name", "value": "Alice"}
]
There are five leaves. active sorts first because a comes before u. Under user, address comes before age because d comes before g in the second letter, and age comes before name.
Input: data = {"items": [10, 20, 30], "meta": {"tags": ["alpha", "beta"]}}
Output: [
{"key": "items.0", "value": 10},
{"key": "items.1", "value": 20},
{"key": "items.2", "value": 30},
{"key": "meta.tags.0", "value": "alpha"},
{"key": "meta.tags.1", "value": "beta"}
]
Each list element gets its index as the last path segment. Every items path sorts before every meta path because i comes before m.
Input: data = {"a_b": null, "a": {"y": [], "x": {}}, "flags": [true, {"on": false}]}
Output: [
{"key": "a.x", "value": {}},
{"key": "a.y", "value": []},
{"key": "a_b", "value": null},
{"key": "flags.0", "value": true},
{"key": "flags.1.on", "value": false}
]
The empty dictionary and the empty list under a are leaves and keep their values. The second element of flags is a dictionary, so it is expanded into flags.1.on. Both a.x and a.y come before a_b because . (code 46) sorts before _ (code 95).