[Tutor] Don't Understand Problem

Steven D'Aprano steve at pearwood.info
Thu Feb 26 12:38:57 CET 2015


On Thu, Feb 26, 2015 at 08:55:55AM +0000, Alan Gauld wrote:

> However remove() may not do what you think. It removes the
> value zero from your list not the first element. I this case they are 
> the same but in other cases they might not be. You may want to use del() 
> instead
> 
> del(unvisited_caves[0])

del is not a function, it is a statement, so it doesn't need the 
brackets (parentheses for any Americans reading).

del unvisited_caves[0]

Although the parens don't do any harm.


To be clear, `del somelist[x]` deletes the xth item from somelist. But 
`somelist.remove(x)` deletes the first item that equals x, equivalent to 
this Python code:

for i in range(len(somelist)):
    if somelist[i] == x:
        del somelist[i]
        break


-- 
Steve


More information about the Tutor mailing list