pop() clarification

Facundo Batista facundo at taniquetil.com.ar
Wed Apr 11 13:07:15 EDT 2007


Scott wrote:

> Now I know list is a bad name, but for the sake of arguement lets assume its 
> not a built in sequence>

It's easier to use another name, than writing all that parragraph, ;)


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

You're reading it everywhere because it's the correct syntax, ;)

> At first I took that to mean that list.pop(i) would return some type of 
> error, but it doesn't.

"i" is enclosed in square brackets because it's optional. So,
list.pop(i) is a correct way to call it, as correct as list.pop().

Take note that list.pop([i]) is *not* the correct way to call it.

I assume you're getting confused by the [], asuming that those means a
list. No. Take a look here:

  http://www.python.org/doc/current/lib/typesseq-mutable.html

As you see there, when an iterable is needed, it just named different,
syntax do not use brackets to mean "list".


> Now I'm not stupid enough to believe that I'm the first to try:
>>>>list = ['this', 'is', 'an', 'example']
>>>>list.pop(1)
> and have it return the desired effect of:
> 'is'
>>>>list
> ['this', 'an', 'example']

But try this also:

>>> l = ['this', 'is', 'an', 'example']
>>> l.pop([2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required

Regards,

-- 
.   Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/





More information about the Python-list mailing list