Ah, after rereading the code what is doing iterating through the list is an unconventional manner, by using the last element and then removing it:

```
while to_list:
    element = to_list[-1]
    ...
    to_list.pop()
```

I have to read more carefully but I don't see a clear reason why is being done like that. A comment near the while can be useful.