Monday, September 8, 2014

An annoying thing with Python slices

You of course know that Python slices are awesome:

a[:3] -> 'ABC'
a[2:5] -> 'CDE'

And more interestingly:

a[-3:] -> 'EFG'


a[6:4:-1] -> 'GF'

But you can see that the reverse slicing is starting to stretch the fence-post we are familiar with. Python uses zero based, inclusive-exclusive indexing. This corresponds to a C syntax of (for i = n; i < m; i++). When you reverse it the slice goes (for i = m - 1; i > n - 1; i--).

As you can imagine this starts to get ugly and at one point it gets to be wrong:

Say, as is often the case, you are not taking static, pre-determined slices but rather slices determined at runtime. Say you are taking slices between n and m or [n, m).

The forward slice is a[n:m]
The backward slice is a[m-1:n-1:-1] right? Because of the fence posts?

Well yes, except what happens when n = 0? The forward slice is fine but the reverse slice resolves to a[m-1:-1:-1]

This is where Python becomes a little too clever. As you will recall from our earlier examples, negative indices indicate offsets from the end of the object. So, the last slice returns empty.

The correct slice is a[m-1:None:-1] or a[m-1::-1] and the logic for this is cumbersome:

a[m-1:n-1 if n > 0 else None:-1]

The simpler way is to do a[n:m][::-1].

No comments:

Post a Comment