471,357 Members | 1,161 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

Difference between del and remove?

Got a quick n00b question. What's the difference between del and
remove?
Dec 13 '07 #1
4 9411

"Yansky" <th*********@gmail.comwrote in message
news:eb**********************************@e25g2000 prg.googlegroups.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.

tjr

Dec 13 '07 #2
On Wed, 12 Dec 2007 19:17:49 -0800, Yansky wrote:
Got a quick n00b question. What's the difference between del and remove?
Everything. list.remove(value) removes the first item equal to value.
del list[index] removes the item at position index.

See also: help([].remove)
Help on built-in function remove:

remove(...)
L.remove(value) -- remove first occurrence of value
>>alist = [101, 102, 103, 104, 105]
alist.remove(103)
alist
[101, 102, 104, 105]
>>del alist[0]
alist
[102, 104, 105]
--
Steven.
Dec 13 '07 #3
Yansky <th*********@gmail.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, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 13 '07 #4
Thanks for the clarification guys. :)

On Dec 13, 7:05 pm, Tim Roberts <t...@probo.comwrote:
Yansky <thegoodd...@gmail.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.
Dec 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by ambar.shome | last post: by
reply views Thread by Shi Mu | last post: by
10 posts views Thread by Jon Davis | last post: by
12 posts views Thread by Q1tum | last post: by
reply views Thread by Beemer Biker | last post: by
5 posts views Thread by Julius | last post: by
11 posts views Thread by sunnyalways4u2000 | last post: by
15 posts views Thread by student4lifer | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.