list.clear() missing?!?
Terry Reedy
tjreedy at udel.edu
Wed Apr 12 21:14:41 EDT 2006
"Peter Hansen" <peter at engcorp.com> wrote in message
news:e1k6ev$42c$1 at sea.gmane.org...
> It's not even clear that extend needs two lines:
>
> >>> s = range(5)
> >>> more = list('abc')
> >>> s[:] = s + more
> >>> s
> [0, 1, 2, 3, 4, 'a', 'b', 'c']
This is not the same as list.extend because it makes a separate
intermediate list instead of doing the extension completely in place.
However, the following does mimic .extend.
>>> s=range(5)
>>> s[len(s):] = list('abc')
>>> s
[0, 1, 2, 3, 4, 'a', 'b', 'c']
So. at the cost of writing and calling len(s), you are correct that .extend
is not necessary.
Terry Jan Reedy
More information about the Python-list
mailing list