list.extend([]) Question

D

Dan Brown

Why does extending a list with the empty list result in None? It
seems very counterintuitive to me, at least --- I expected ['a'].extend
([]) to result in ['a'], not None.
 
A

Alf P. Steinbach

* Dan Brown:
Why does extending a list with the empty list result in None? It
seems very counterintuitive to me, at least --- I expected ['a'].extend
([]) to result in ['a'], not None.

It does.

'extend' is an operation that /modifies/ the array.

It just returns None as its expression result, in the same way as e.g. the
Python 3.x 'print' (another pure "doer" operation).
>>> L = ['a']
>>> L ['a']
>>> L2 = L.extend( [] )
>>> L2
>>> L2 is None True
>>> L ['a']
>>> _


Cheers & hth.,

- Alf
 
A

Andre Engels

Why does extending a list with the empty list result in None?  It
seems very counterintuitive to me, at least --- I expected ['a'].extend
([]) to result in ['a'], not None.

Extend is a method of the list. The list itself is changed, it does
not return itself:
A = [1,2]
B = [3,4]
C = A.extend(B)
C
C is None True
A
[1, 2, 3, 4]


Thus, nothing special about extend([]), this is the general behaviour of extend
 
D

Dan Brown

It does.

'extend' is an operation that /modifies/ the array.

It just returns None as its expression result, in the same way as e.g. the
Python 3.x 'print' (another pure "doer" operation).

   >>> L = ['a']
   >>> L
   ['a']
   >>> L2 = L.extend( [] )
   >>> L2
   >>> L2 is None
   True
   >>> L
   ['a']
   >>> _

Cheers & hth.,

- Alf

Aha. Well, I feel a bit silly for not thinking to try it that way.
Thanks!
 
S

Steve Holden

Dan said:
Why does extending a list with the empty list result in None? It
seems very counterintuitive to me, at least --- I expected ['a'].extend
([]) to result in ['a'], not None.

How very inconvenient of Python! What it actually does is create an
anonymous list containing only the element 'a', and leave it unchanged
by extending it with an empty list. Since there is no longer any
reference to the list it has become garbage.

Contrast that with:
lst = ['a']
lst.extend([])
lst ['a']
lst.append([])
lst ['a', []]
lst.extend(['1'])
lst ['a', [], '1']

As you can see by the absence of output, both the .extend() and
..append() list methods return None. They mutate the list instance upon
which they are called.

In your example you were expecting the methods to return the mutated
list. They don't.

regards
Steve
 
G

Gerald Britton

I think it's because when you do ['a'].extend([]) or whatever, the
result is whatever the method "extend" returns. But "extend" has no
return value, hence you will see None if you do this interactively.

Why does extending a list with the empty list result in None?  It
seems very counterintuitive to me, at least --- I expected ['a'].extend
([]) to result in ['a'], not None.

http://www.python.org/doc/faq/general/#why-doesn-t-list-sort-return-the-sorted-list
 
B

Bruno Desthuilliers

Gerald Britton a écrit :
I think it's because when you do ['a'].extend([]) or whatever, the
result is whatever the method "extend" returns. But "extend" has no
return value

It does : it returns None.
 
J

Jack Diederich

I think it's because when you do ['a'].extend([]) or whatever, the
result is whatever the method "extend" returns.  But "extend" has no
return value, hence you will see None if you do this interactively.

That sums it up. In Python the convention is to raise an exception on
error, return a new value in the case where a new value is created,
and - as in this case - to return None for modification of an existing
value. Returning "None" is vexing if you are used to another language
that has a different convention but is expected for well behaved
python libraries.

So yeah, Python does it that way because the intent is to loudly and
regularly announce that something was modified or to loudly and
regularly announce that something new was /not/ created. It's a
boring story with no whiz-bang feature behind it, but I like that the
language behaves that way for exactly that reason.

-Jack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top