Arrays?

John Hunter jdhunter at nitace.bsd.uchicago.edu
Wed Nov 13 15:34:26 EST 2002


>>>>> "Wojtek" == Wojtek Walczak <gminick at hacker.pl> writes:

    >>>> a=[[]]*2 a
    Wojtek> [[], []]
    >>>>

    Wojtek> Is that what you wanted?

Probably not, since this is the famous empty list gotcha (see the
recent thread 'pitfall for your amusement",
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&frame=right&th=967b1bba3344d6a9&seekm=yu993cq6hels.fsf%40europa.research.att.com#link10

The problem is that both elements of the list are the same object
instance [], so changing one changes the other, which is usually not
what you want

>>> a=[[]]*2 
>>> a[0].append('Hi mom')
>>> a
[['Hi mom'], ['Hi mom']]


John Hunter




More information about the Python-list mailing list