Newbie: tuple list confusion.

John Roth newsgroups at
Mon Sep 13 03:58:46 CEST 2004

"Q852913745" <q852913745 at> wrote in message 
news:20040912195529.03437.00000338 at
>I am trying to learn python from 'Python programming for the absolute 
> I'm sure I understand the difference between tuples and lists, but while
> experimenting with what I have learned so far, I was suprised that this 
> code
> worked.
> # Create list a
> a=["start",878,"item 1",564354,"item 2"]
> print "length of list a =",len(a)
> print "a =",a ; print
> # Create tuple b
> b=(234,"item 3",456,"end")
> print "length of tuple b =",len(b)
> print "b =",b ; print
> print "Add b onto a"  # Shouldn't work!
> #---------------------------------------------
> # a=a+b    # causes an error as it should,
> a+=b      # but why is this ok?
> #---------------------------------------------
> print "a =",a
> print "length of a =",len(a)
> print;c=raw_input("Press enter to exit")
> My book states that a=a+b and a+=b are the same, and that different 
> 'types' of
> sequence cannot be joined together.

This is a simplification. a+=b can be done with an in-place update
for mutable objects, such as lists. An in place update doesn't have
to do exactly the same operation as the equivalent binary operator,
and it seems like it doesn't here.

Since it's an in-place operation, I would expect that a+=b
would be the same as a.append(b), but I haven't checked
that this is the case, and the manual doesn't indicate that
append() accepts general sequences. It does, however,
indicate that at one time it had to accept tuples, and likely
the code to do that was never removed. See note 2
in the Library Reference Manual under Builtin Objects -
Builtin Types - Sequence Types - Mutable Sequence Types.

I wouldn't depend on this working in future  releases.

John Roth 

More information about the Python-list mailing list