# Newbie: tuple list confusion.

Dan Perl dperl at rogers.com
Mon Sep 13 04:11:22 CEST 2004

```"John Roth" <newsgroups at jhrothjr.com> wrote in message
news:10k9vnqkfieasd7 at news.supernews.com...
>
> "Q852913745" <q852913745 at aol.com> wrote in message
> news:20040912195529.03437.00000338 at mb-m27.aol.com...
>>I am trying to learn python from 'Python programming for the absolute
>>beginner'
>> 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

John,

I don't think it's an 'append', it's probably an 'extend'.
a=[1,2,3]
a.append((4,5))
print a      #  [1, 2, 3, (4, 5)]
a.pop()    #  (4, 5)
a.extend((4,5))
print a     #  [1, 2, 3, 4, 5]

```