List comprehensions' ugliness (Was: Re: How to explain exactly what "def" does?)

Tim Peters tim.one at comcast.net
Thu Feb 6 21:12:42 EST 2003


[Donnal Walter]
> mylist = mylist + otherlist
>
> The above assignment statement is an idiom that I have (perhaps naively)
> adopted. Is the .extend syntax more efficient?

Yes.  However,

    mylist += otherlist

is equivalent to

    mylist.extend(otherlist)

> I can also see where it might be considered more readable or more
> accurate. (?)

It has different semantics.  If it's your intent to extend a list in-place,
then .extend or += are necessary.  If it's your intent to create a new list
object, then .extend and += must not be used.  If it doesn't matter to your
app whether you create a new object, suit yourself.






More information about the Python-list mailing list