Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > list.extend([]) Question

Reply
Thread Tools

list.extend([]) Question

 
 
Dan Brown
Guest
Posts: n/a
 
      01-30-2010
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-30-2010
* 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
 
Reply With Quote
 
 
 
 
Andre Engels
Guest
Posts: n/a
 
      01-30-2010
On Sat, Jan 30, 2010 at 4:32 PM, Dan Brown <(E-Mail Removed)> wrote:
> 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

--
André Engels, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Dan Brown
Guest
Posts: n/a
 
      01-30-2010
On Jan 30, 8:38*am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> 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!
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      01-30-2010
Dan Brown wrote:
> 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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      02-05-2010
In article <(E-Mail Removed)>,
Dan Brown <(E-Mail Removed)> wrote:
>
>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/genera...he-sorted-list
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

import antigravity
 
Reply With Quote
 
Gerald Britton
Guest
Posts: n/a
 
      02-05-2010
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.

On Fri, Feb 5, 2010 at 10:55 AM, Aahz <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>,
> Dan Brown *<(E-Mail Removed)> wrote:
>>
>>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/genera...he-sorted-list
> --
> Aahz ((E-Mail Removed)) * * * * * <*> * * * * http://www.pythoncraft.com/
>
> import antigravity
> --
> http://mail.python.org/mailman/listinfo/python-list
>




--
Gerald Britton
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      02-05-2010
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.

 
Reply With Quote
 
Jack Diederich
Guest
Posts: n/a
 
      02-05-2010
On Fri, Feb 5, 2010 at 11:31 AM, Gerald Britton
<(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments