Back to Practice
#0006
Parse Key-Value Lines
MediumPython18 min15 XP
Problem
Given text with one key=value pair per line, return a dictionary. Ignore blank lines. Strip spaces around keys and values.
Why This Matters
A lot of real scripting is turning messy text into structured data. This problem teaches careful splitting and whitespace handling.
Function Signature
def parse_settings(text):
Examples
Example 1
Input"lr = 0.1\nepochs=10\n\nmodel = linear"
Output{"lr": "0.1", "epochs": "10", "model": "linear"}
Blank lines are skipped and spaces around keys/values are removed.
Constraints
- Every non-blank line contains =.
- Values should remain strings.
- Split on the first = only.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Spaces and blank line
Input"lr = 0.1\nepochs=10\n\nmodel = linear"
Expected{"lr": "0.1", "epochs": "10", "model": "linear"}
split("=", 1) avoids breaking values that may contain another equals sign.
Equals sign inside value
Input"query=a=b"
Expected{"query": "a=b"}
Only the first equals sign separates key from value.
Hidden Test Categories
Leading and trailing blank linesValues with spacesSingle settingDuplicate keys where later value wins