[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Surprised by the command "del"

K Viltersten

3/1/2008 8:06:00 PM

I'm reading the docs and at 5.2 the del
statement is discussed. At first, i thought
i've found a typo but as i tried that
myself, it turns it actually does work so.

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?!

Elaboration:
I wonder why such an unintuitive effect has
been implemented. I'm sure it's for a very
good reason not clear to me due to my
ignorance. Alternatively - my expectations
are not so intuitive as i think. :)

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

3 Answers

Dennis Lee Bieber

3/1/2008 8:11:00 PM

0

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/

Jerry McEwen

3/1/2008 10:42:00 PM

0

On Sat, 1 Mar 2008 21:05:41 +0100, "K Viltersten"
<tmp1@viltersten.com> wrote:

>I'm reading the docs and at 5.2 the del
>statement is discussed. At first, i thought
>i've found a typo but as i tried that
>myself, it turns it actually does work so.
>
> 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?!
>
>Elaboration:
>I wonder why such an unintuitive effect has
>been implemented. I'm sure it's for a very
>good reason not clear to me due to my
>ignorance. Alternatively - my expectations
>are not so intuitive as i think. :)


I think it should say
del a[1:2]
then it works


K Viltersten

3/2/2008 12:32:00 AM

0

>>I'm reading the docs and at 5.2 the del
>>statement is discussed. At first, i thought
>>i've found a typo but as i tried that
>>myself, it turns it actually does work so.
>>
>> 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?!
>>
>>Elaboration:
>>I wonder why such an unintuitive effect has
>>been implemented. I'm sure it's for a very
>>good reason not clear to me due to my
>>ignorance. Alternatively - my expectations
>>are not so intuitive as i think. :)
>
> I think it should say
> del a[1:2]
> then it works


While i'm thankful for the advice, i need to
point out that the question wasn't "how to"
but "why". Anyhow, it's been explained as a
matter of definition of a "slice".

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy