Back to Practice
#0111
Nested Get With Dict Keys and List Indexes
MediumPython25 min20 XP
Problem
Given data, a path containing dictionary keys or list indexes, and default, walk the path. Return default if a key/index is missing or the type does not match.
Why This Matters
Real API responses mix dictionaries and lists. Safe traversal prevents brittle key/index errors in scripts and data pipelines.
Function Signature
def deep_get(data, path, default=None):
Examples
Example 1
Input{"users": [{"name": "Asha"}]}, ["users", 0, "name"]
Output"Asha"
The path enters a dict, then list index 0, then another dict.
Constraints
- Return the requested value exactly.
- Handle the stated edge cases.
- Keep the solution readable.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input{"users": [{"name": "Asha"}]}, ["users", 0, "name"]
Expected"Asha"
The path enters a dict, then list index 0, then another dict.
Hidden Test Categories
empty pathwrong type in middlenegative index rejected