List slices

Alex not_such_a_brain_after_all at mit.edu
Tue Oct 31 10:44:14 EST 2000


> Given the list
> 
>     x = ['first','second','third','forth']
> 
> and
> 
>     x[-1]
> 
> returns ['forth'], it seems inconsistent that
> 
>     x[1:-1]
> 
> returns ['second',third'] instead of the expected
> ['second',third','forth'].  Can someone provide an explanation for
> this please?

If x is a python sequence, x[start: end] returns the list 
[x[start], x[start+1], ... x[end-1]]

HTH
Alex.

-- 
Speak softly but carry a big carrot.




More information about the Python-list mailing list