The `.get` doesn't put the `w -> []` mapping into locations.

You should use:

```
locations.setdefault(w, []).append(i)
```