pop() clarification

Hamilton, William whamil1 at entergy.com
Wed Apr 11 12:56:28 EDT 2007


> -----Original Message-----
> From: python-list-bounces+whamil1=entergy.com at python.org
[mailto:python-
> list-bounces+whamil1=entergy.com at python.org] On Behalf Of Scott
>
> I understand all that.  What I don't understand is why all the
> documentation
> I see says, "When removing a specific element from a list using pop()
it
> must be in this format: list.pop([i]).
> At first I took that to mean that list.pop(i) would return some type
of
> error, but it doesn't.
> I can't find any documentation saying that this rule that I keep
reading
> about (again list.pop([i]) ) is the only format to use when removing a
> specific element because......with the explaination to follow.

I believe that the [i] notation is to indicate that it has to be a valid
index to your list.  If i isn't a valid index, you get an IndexError.

>>> spam=['this', 'is', 'a', 'list']
>>> spam.pop(1)
'is'
>>> spam.pop(4)

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in -toplevel-
    spam.pop(4)
IndexError: pop index out of range


---
-Bill Hamilton



More information about the Python-list mailing list