Shouldn't it be:
```
if parent_id and parent_id in pages:
```

If pages = [1, 3, 5] and page 3 is the parent of page 1, the dict will have an empty list for page 3.