# Count first letter in dictionary

Hi all,

I´m having trouble with the last challenge in Code Challenge: Dictionaries, this is:

After a while struggling with it, I ended up looking the solution, but I still can´t wrap my head around it.

First, unless I´m missing something out, the tip and the solution don´t match.

The tip says:

Begin by creating an empty dictionary named something like `letters` . Loop through the keys of `names` and access the first letter of each the key using `key[0]` .

*If that letter is not a key in `letters` , create a new key/value pair where the key is `key[0]` and the value is *the length of `names[key]**` .

If that letter is a key in `letters` , simply add the length of `names[key]` to value associated with `key[0]` in `letters` .

I´d say that the resulting code would be something like this:

``````def count_first_letter(names):
letters = {}
for key in names.keys():
first_letter = key[0]
if first_letter not in letters:
letters[first_letter] = len(names[key])
if first_letter in letters:
letters[first_letter] += len(names[key])

return letters

``````

However, if I execute this with the second example (Stark, Snow, Sannister), I get a count of 10 instead of 7, which I don´t understand. When it iterates through the elements, the first time it encounters a last name starting with S (Stark), it´s not in the dictionary, therefore it should only add 3 (the number of elements in Stark). Then, with Snow and Sannister, since the dictionary already has accounted for a last name starting with S, it should simply add 1 and 3, for a total of 7. Then why am I getting 10?

Then, if I check the solution, I see that the code is not what the tip hints at, what i get is:

``````def count_first_letter(names):
letters = {}
for key in names.keys():
first_letter = key[0]
if first_letter not in letters:
letters[first_letter] = 0
if first_letter in letters:
letters[first_letter] += len(names[key])

return letters
``````

The difference would be that, when iterating and encountering for the first time a last name with a given initial, the value for that key is 0. Then I don´t understand why the code produces the right solution! With the same dictionary, when iterating through the 3 keys, the first one (Stark) should produce “S:0”, then it would add up the values of Snow and Sannister to a total of 4. I don´t understand why the result is 7.

I feel I´m missing something obvious here, anybody can help out?

Thanks a lot!!

we could use print to inspect our code:

``````# Write your count_first_letter function here:
def count_first_letter(names):
letters = {}
for key in names.keys():
first_letter = key[0]
if first_letter not in letters:
print(key)
letters[first_letter] = len(names[key])
if first_letter in letters:
print(key)
letters[first_letter] += len(names[key])
return letters
``````

which reveals are very interesting clue. We see Stark and Lannister twice.

How come? Walk through the flow your code. We check if the (first) letter is not in letters. Which is then added to letters dictionary

now we are going to check if (first) letter is in letters. It is, we just added, so now the first name count is added again

2 Likes

Hi stetim94, thanks a lot for answering.

It makes sense now
Again, thanks!!

1 Like