Sort a part of a list in place

I’d write it this way:

a[i:j] = sorted(a[i:j])

It is not in-place sort either, but fast enough for relatively small segments.

Please note, that Python copies only object references, so the speed penalty won’t be that huge compared to a real in-place sort as one would expect.

Leave a Comment