Back to Practice
#0129

Flatten a Dictionary One Level

MediumPython25 min20 XP

Problem

Given a dictionary, flatten only values that are dictionaries. Use parent.child keys for nested fields.

Why This Matters

Flattening one level is common when preparing JSON-like API data for tables or logs.

Function Signature

def flatten_one_level(data):

Examples

Example 1
Input{"user": {"id": 1, "name": "Asha"}, "active": True}
Output{"user.id": 1, "user.name": "Asha", "active": True}

Only the nested user dictionary is expanded.

Constraints

  • Return the exact requested structure.
  • Handle normal edge cases cleanly.
  • Prefer readable code over clever shortcuts.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input{"user": {"id": 1, "name": "Asha"}, "active": True}
Expected{"user.id": 1, "user.name": "Asha", "active": True}

Only the nested user dictionary is expanded.

Hidden Test Categories
no nested dictionariesempty nested dictionarymultiple nested dictionaries