Surprised by the command "del"

C

castironpi

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.   :)

It's the Phillips vs. the flathead. Is b in x[a:b:c] an endpoint or a
distance?

This is Python too; you're welcome to implement:
- b means length
- one-based indices
- inclusive
- b out of bounds exceptions

A slice object (a,b,c) is passed to
whatevercollection.__getitem__, .__setitem__, or .__delitem__.
 
S

Steve Holden

K said:
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. :)
It deletes all the elements referred to by the slice. But since 2-2==0,
there are zero elements in the slice, and the list is unchanged when all
zero of them have been deleted:
>>> a = ["alpha", "beta", "gamma"]
>>> a[2:2] []
>>>

You would have got the result you expected with

del a[2]

or

del a[2:3]

or

del a[-1]

or ...

Remember that slices are specified as half-open intervals. So a[m:n]
includes m-n elements, those indexed from m to n-1.

regards
Steve
 
K

K Viltersten

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. :)
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
K

K Viltersten

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

Remember that slices are specified as half-open
intervals. So a[m:n] includes m-n elements,
those indexed from m to n-1.

Got it. Thanks!
 
F

From

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

K Viltersten

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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top