Dennis Lee Bieber
3/1/2008 8:11:00 PM
On Sat, 1 Mar 2008 21:05:41 +0100, "K Viltersten" <tmp1@viltersten.com>
declaimed the following in comp.lang.python:
> a = ["alpha", "beta", "gamma"]
> del a[2:2]
> a
>
> Now, i expected the result to be that the
> "beta" element has been removed. Obviously,
> Python thinks otherwise. Why?!
>
Well... The first problem is that subscript 2 is "gamma", not
"beta".
Lists index from 0
(used fixed width font)
['alpha', 'beta', 'gamma']
^ ^ ^
0 1 2
[2:2]
says start from the "split" before element 2, and END at the "split"
before element 2.
To remove "beta" you need to specify the "split" before element 1,
and end at the split before element 2...
>>> a
['alpha', 'beta', 'gamma']
>>> del a[1:2]
>>> a
['alpha', 'gamma']
>>> del a[1:2]
>>> a
['alpha']
>>> del a[1:2]
>>> a
['alpha']
>>>
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/