Skip to content

Dictionaries: look up by key

Read · 1 of 4

A dict maps keys to values

count = {} starts empty. count["mango"] = 1 stores a value for a key. count.get(key, 0) reads it, or gives 0 if the key isn't there yet.

So count[w] = count.get(w, 0) + 1 counts things in one line. for key, value in count.items(): visits every pair.