[Tutor] Explanation of Lists data Type

Alan Gauld alan.gauld at freenet.co.uk
Thu Apr 13 23:22:34 CEST 2006


>> list [-3:3]
>> which is an error but is not reported as such, rathger it returns an 
>> empty list!
>
> I don't think this is an error, and whether it returns an empty list or 
> not depends on the length of the list:

I stand corrected, and yes when you think about it, it makes sense.

> print i, range(i)[-3:3]
> 0 []
> 1 [0]
> 2 [0, 1]
> 3 [0, 1, 2]
> 4 [1, 2]
> 5 [2]
> 6 []

[-3:3] is not an error as such it just doesn't make sense for longer lists
since -3 is after 3. But for short lists -3 sits before 3 so the slice 
between
them is valid.

Interesting, and shows the danger of relying on inaccurate analagies
like my circular list. It works in many cases but not all!

Thanks Roel.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list