sd44 wrote: > In <learning python> > part III > how does Python handle it if you try to extract a sequence in reverse, > with the lower bound greater than the higher bound (e.g., L[3:1])? Hint: > try > assigning to this slice (L[3:1]=['?']), and see where the value is put. > > >>>> L=[1,2,3,4] > >>>> L[3:1] > [] >>>> print L > [1, 2, 3, 4] >>>> L[3:1]=['?'] >>>> print L > [1, 2, 3, '?', 4]
To specify a backwards slice, you have to supply the third slice argument: L[3:1:-1] As it stands, `L[3:1]=['?']` replaces the empty sequence at index 3 with the contents of ['?']. You know that `L[3:1]` is empty from the display above the print statement. The language manual has slicing defined as "a[i:j] selects all items with ndex k such that i <= k < j". This is true as far as it goes but the tutorial is truly on to something when it says "see where the value is put." Python won't build a sequence that's less-than-empty, so it deals with an empty sequence here. We're probably due for an argument on whether it should raise an exception instead. See the current thread on whether `+` should be a sequence concatenation operator. Mel. -- http://mail.python.org/mailman/listinfo/python-list