Generating a random subsequence

Hans Nowak wurmy at earthlink.net
Wed Jan 9 00:27:24 EST 2002


Tom Bryan wrote:
> 
> Alex Martelli wrote:
> > The += augmented operator dispatches appropriately to in-place or
> > not-in-place mutation depending on LHO's mutable/immutable nature.
> 
> Is that just true of +=?
> 
> That is, if returnSeq is a list then, I can say
> returnSeq = returnSeq + item
> OR
> returnSeq.append( item )
> OR
> returnSeq += item
> 
> You're saying that the last one is roughly equivalent to the second one and
> not the first?  Or are they all three equivalent?

No, += for lists is actually equivalent to list.extend(). So

  mylist += item

will only work if 'item' is a list itself. (That is what
list.extend expects, too.) So these two are equivalent:

  mylist += yourlist
  mylist.extend(yourlist)

and while

  mylist = mylist + yourlist

returns the same result, it works differently internally.
+= and extend change the list in-place; the x = x + y
idiom creates a new list and rebinds it to x.

--Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==') 
       # decode for email address ;-)
Site:: http://www.awaretek.com/nowak/



More information about the Python-list mailing list