Well, I think that lines 261:264 would be clearer if they were replaced by something like (untested):

```python
unnecessary_permutations = [(x, mapping[x])]
del mapping[x]
mapping = sorted(mapping.items(), key=_sort_key, reverse=True)
```
and BTW I don't like the name "unnecessary_permutations", but I don't really have a better idea ("fixed_mapping" maybe?).