preferred syntax for list extend?

Ian Bicking ianb at colorstudy.com
Wed Apr 17 14:29:02 EDT 2002


On Wed, 2002-04-17 at 03:01, Ante wrote:
> 
> <bvdpoel at uniserve.com> wrote in message
> news:3CBCC196.466476CE at uniserve.com...
> >
> > Just doing a bit of fooling around and I've found that if:
> >
> > q1=[1,2,3,4]
> > q2=[5,6,7,8]
> >
> > then:
> >
> > q1 += q2
> >
> > is about 15% faster than
> >
> > q1.extend(q2)
> >
> > Mind you, my test was with a relatively short list, don't know if longer
> > lists would act differently.
> >
> > I would have thought that using extend() would be preferred and faster?
> 
> Even if it was faster, I would still prefer +=.
> foo.extend(yada) doesn't look beautiful to me at all.
> After all, why do we have, e.g. == operator so we can say if a == b and
> not a.equals(b)? Because the former is simply better looking, and to me the
> same applies to += versus .extend() or even .append()

+= is different from .extend()... for instance:

q1 = [1, 2, 3]
q2 = q1
q3 = [4, 5, 6]

q1 += q3   # then...
q2 == [1, 2, 3]

But...

q1 = [1, 2, 3]
q2 = q1
q3 = [4, 5, 6]

q1.extend(q3)
q2 == [1, 2, 3, 4, 5, 6]

  Ian







More information about the Python-list mailing list