How does the provided solution create new keys?

word_lengths[word] = len(word)
return word_lengths

I do not understand the solution provided in this exercise. Why/how does word_lengths[word] = len(word) add a new key?

Because that’s the way (well, one way) that you add a new key to a dictionary. If word is not already a key, word_lengths[word] = len(word) will add it, with the associated value len(word).

If word it is already a key, the statement word_lengths[word] = something will overwrite the current value with whatever is on the right side of the assignment statement.

my_dict = {}

word = "ant"

my_dict[word] = len(word)

my_dict[word] = len(word) * 2    # Different value here so you can see a change

my_dict[word]  += 10        # This is more interesting!


{'ant': 3}
{'ant': 6}
{'ant': 16}