array alice of [:-0] ??

Antoon Pardon apardon at forel.vub.ac.be
Tue Jul 18 09:29:15 EDT 2006


On 2006-07-18, guthrie <guthrie at mum.edu> wrote:
> Beginner question! :-)
>
> x=[1,2,3,4]
> for i in range(len(x)):
>     print x[:-i]
>
> >>> []
> >>> [1,2,3]
> >>> [1,2]
> >>> [1]
>
> 1) The x[:-0] result seems inconsistent to me;
>      I get the idea that -0=0, so it is taken as x[:0] -> []

That is correct. Negative indexes are a handy shorthand, but
they can give unexpected/strange results in a number of cases.

> 2) how then should one do this basic left-recursive subsetting (easily).

  x=[1,2,3,4]
  lng = len(x)
  for i in range(lng):
      print x[:lng-i]

-- 
Antoon Pardon



More information about the Python-list mailing list