append to a sublist - please help

Arnaud Delobelle arnodel at googlemail.com
Sun Apr 6 12:20:56 EDT 2008


On Apr 6, 5:16 pm, Helmut Jarausch <jarau... at skynet.be> wrote:
> Hi,
>
> I must be blind but I don't see what's going wrong
> with
>
> G=[[]]*2
>
> G[0].append('A')
> G[1].append('B')
> print G[0]
>
> gives
>
> ['A', 'B']
>
> as well as
> print G[1]
>
> I was expecting
> ['A']
> and
> ['B']
> respectively.
>
> Many thanks for enlightening me,
> Helmut.
>
> --
> Helmut Jarausch
>
> Lehrstuhl fuer Numerische Mathematik
> RWTH - Aachen University
> D 52056 Aachen, Germany

This is in the top two FAQ, here is the relevant section:

http://www.python.org/doc/faq/programming/#how-do-i-create-a-multidimensional-list

HTH

--
Arnaud




More information about the Python-list mailing list