invert or reverse a string... warning this is a rant

Carl Banks pavlovevidence at gmail.com
Fri Oct 20 11:15:03 EDT 2006


skip at pobox.com wrote:
> The extended slice notation comes from the
> numeric community though where they are probably all former FORTRAN
> programmers.  I think the concept of start, stop, step (or stride?) is
> pretty common there.

Yep.  I do a bit of numerical work and the meaning of abc[::-1] is
plain as day to me.

One thing I'd like to point out is that few people seem to be thrown
off when slices are used to twiddle list elements.  A few days back
there was a thread about reordering a list such as [1,2,3,4,5,6,7,8,9]
as [1,4,7,2,5,8,3,6,9], and the advice was along the lines of
a[0::3]+a[1::3]+a[2::3].  I don't remember any complaints about the the
notation there.

I think the problem here isn't the slice notation; it's the use of
slice notation for something that seems conceptually distinct.  People
who aren't used to doing a lot of slicing don't always have the
connection between "reversing" and "slicing" in mind.  It wouldn't
occur to them to accomplish a reverse with a slice, and it would
surprise them a bit to see it.


Carl




More information about the Python-list mailing list