question about list extension

Terry Reedy tjreedy at udel.edu
Fri Apr 16 15:16:55 EDT 2010


On 4/16/2010 9:41 AM, J wrote:
> Ok... I know pretty much how .extend works on a list... basically it
> just tacks the second list to the first list... like so:
>
>>>> lista=[1]
>>>> listb=[2,3]
>>>> lista.extend(listb)
>>>> print lista;
> [1, 2, 3]

This shows right here that lista is extended in place. If you are not 
convinced, print(id(lista)) before and after.

> what I'm confused on is why this returns None:
>
>>>> lista=[1]
>>>> listb=[2,3]
>>>> print lista.extend(listb)
> None

It is conventional in Python (at least the stdlib) that methods that 
mutate mutable objects 'in-place' return None to clearly differentiate 
them from methods that return new objects. There are pluses and minuses 
but such it is.

Terry Jan Reedy




More information about the Python-list mailing list