"Yansky" <thegooddale@gm ail.comwrote in message
news:eb0cc538-c1bc-470d-a863-afd15ccafedb@e2 5g2000prg.googl egroups.com...
| Got a quick n00b question. What's the difference between del and
| remove?
Python has a del statement but not a remove statement. Correspondingly ,
the first is a keyword and the second is not.
>
>Got a quick n00b question. What's the difference between del and
>remove?
It would have been easier to answer if you had given a little context.
"del" is a Python statement that removes a name from a namespace, an item
from a dictionary, or an item from a list.
"remove" is a member function of the 'list' class that finds a specific
entry in the list and removes it.
Example:
>>e = [9,8,7,6] ; del e[2] ; e
[9, 8, 6]
>>e = [9,8,7,6] ; e.remove(2) ; e
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: list.remove(x): x not in list
>>e = [9,8,7,6] ; e.remove(8) ; e
[9, 7, 6]
Note that "del e[2]" removed item number 2 (counting from 0). e.remove(8)
removed the item that had the value 8 from the list. e.remove(2) failed
because the number 2 was not in the list.
Dictionaries do not have a "remove" method. You have to use the "del"
statement.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
On Dec 13, 7:05 pm, Tim Roberts <t...@probo.com wrote:
Yansky <thegoodd...@gm ail.comwrote:
>
Got a quick n00b question. What's the difference between del and
remove?
>
It would have been easier to answer if you had given a little context.
>
"del" is a Python statement that removes a name from a namespace, an item
from a dictionary, or an item from a list.
>
"remove" is a member function of the 'list' class that finds a specific
entry in the list and removes it.
>
Example:
>
>e = [9,8,7,6] ; del e[2] ; e
>
[9, 8, 6]
>
>e = [9,8,7,6] ; e.remove(2) ; e
>
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: list.remove(x): x not in list
>
>e = [9,8,7,6] ; e.remove(8) ; e
>
[9, 7, 6]
>
Note that "del e[2]" removed item number 2 (counting from 0). e.remove(8)
removed the item that had the value 8 from the list. e.remove(2) failed
because the number 2 was not in the list.
>
Dictionaries do not have a "remove" method. You have to use the "del"
statement.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
Comment