List comprehensions' ugliness (Was: Re: How to explain exactly what "def" does?)
Duncan Booth
duncan at NOSPAMrcp.co.uk
Thu Feb 6 07:21:57 EST 2003
Donnal Walter <donnal at donnal.net> wrote in
news:mailman.1044525858.22549.python-list at python.org:
>> mylist.extend(otherlist) is better.
>>
>
> mylist = mylist + otherlist
>
> The above assignment statement is an idiom that I have (perhaps naively)
> adopted. Is the .extend syntax more efficient? I can also see where it
> might be considered more readable or more accurate. (?)
Note that these do different things:
mylist.extend(otherlist)
modifies mylist in place, so any other reference to mylist will also see
the new value. This may be more efficient if mylist doesn't have to be
copied (although increasing its size is still likely to involve a copy
somewhere), but it can have unwanted side effects.
mylist = mylist + otherlist
creates a new list so other references to the object are not affected.
mylist += otherlist
is the same as the former if mylist is of type list, but other sequences
may regard it as the same as the latter.
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list