Twenty lessons of primitives. Today's exercise composes five of them on a small generic input — no new concepts, just careful arrangement.
Given a multi-line string of key,number pairs, build a dictionary mapping each key to the sum of its numbers:
fruits,3
fruits,5
veggies,2
fruits,1
veggies,4
Expected result: {'fruits': 9, 'veggies': 6}.
I split the string into lines, then split each line on the comma, then... how do I accumulate per-key?
Walk through it line by line. For each key, num pair: if the key is already in your result dict, add to its current total; if not, start it at this number. The in operator from week 2 plus dict assignment from yesterday do exactly that.
Two new things at once — in on a dict, and reading the existing value before adding.
in on a dict checks keys, not values — "fruits" in result is True if "fruits" is a key. The shape:
if key in result:
result[key] = result[key] + num
else:
result[key] = numAnd the int() cast — because split returns strings, but I want to add numbers.
That's the only conversion you need. int("3") is 3. After the loop, print(result) shows the dict in insertion order, which matches what we want.
| Primitive | From | Used for |
|---|---|---|
str.splitlines() / split() | L20 | breaking the input into lines, then each line into key + number |
int(...) | L3 (numbers context) | converting the number string to an actual int |
| dictionaries | L15 | accumulating per-key totals |
in operator on dicts | L16 (and L12 for lists) | checking whether the key already has a total |
if / else | L8 | first-time-vs-subsequent branching |
for loop | L13 | walking through the lines |
No new concepts — just arranging the ones you have.
This shape comes up constantly in real Python:
result = {}
for key, value in pairs:
if key in result:
result[key] = result[key] + value
else:
result[key] = valueLater you'll meet shorter forms (result.get(key, 0) + value, defaultdict(int)), but the explicit version above is what every beginner writes first — and what's clearest to read.
inWeek 2's in operator on lists tested element membership. On dicts it tests key membership. Different containers, same operator, different semantics — Python is consistent that way. When in doubt: in always checks the "natural lookup index" of a container (positional for lists, keys for dicts).
Twenty lessons of primitives. Today's exercise composes five of them on a small generic input — no new concepts, just careful arrangement.
Given a multi-line string of key,number pairs, build a dictionary mapping each key to the sum of its numbers:
fruits,3
fruits,5
veggies,2
fruits,1
veggies,4
Expected result: {'fruits': 9, 'veggies': 6}.
I split the string into lines, then split each line on the comma, then... how do I accumulate per-key?
Walk through it line by line. For each key, num pair: if the key is already in your result dict, add to its current total; if not, start it at this number. The in operator from week 2 plus dict assignment from yesterday do exactly that.
Two new things at once — in on a dict, and reading the existing value before adding.
in on a dict checks keys, not values — "fruits" in result is True if "fruits" is a key. The shape:
if key in result:
result[key] = result[key] + num
else:
result[key] = numAnd the int() cast — because split returns strings, but I want to add numbers.
That's the only conversion you need. int("3") is 3. After the loop, print(result) shows the dict in insertion order, which matches what we want.
| Primitive | From | Used for |
|---|---|---|
str.splitlines() / split() | L20 | breaking the input into lines, then each line into key + number |
int(...) | L3 (numbers context) | converting the number string to an actual int |
| dictionaries | L15 | accumulating per-key totals |
in operator on dicts | L16 (and L12 for lists) | checking whether the key already has a total |
if / else | L8 | first-time-vs-subsequent branching |
for loop | L13 | walking through the lines |
No new concepts — just arranging the ones you have.
This shape comes up constantly in real Python:
result = {}
for key, value in pairs:
if key in result:
result[key] = result[key] + value
else:
result[key] = valueLater you'll meet shorter forms (result.get(key, 0) + value, defaultdict(int)), but the explicit version above is what every beginner writes first — and what's clearest to read.
inWeek 2's in operator on lists tested element membership. On dicts it tests key membership. Different containers, same operator, different semantics — Python is consistent that way. When in doubt: in always checks the "natural lookup index" of a container (positional for lists, keys for dicts).
Create a free account to get started. Paid plans unlock all tracks.