[Tutor] Generate array, different results

eryksun eryksun at gmail.com
Fri Sep 21 21:44:49 CEST 2012


On Fri, Sep 21, 2012 at 2:47 PM, Pavel Andreev <apn06y at gmail.com> wrote:
>
> f=[[0]*3]*3                      # array initialization

The above isn't giving you what you want. All 3 sublists are the same object:

    >>> f = [[0]*3]*3

    >>> map(id, f)
    [149391980, 149391980, 149391980]

Do this instead:

    >>> f = [[0]*3 for i in range(3)]

Each iteration in the comprehension creates a new list:

    >>> map(id, f)
    [149392364, 149392012, 149392428]


More information about the Tutor mailing list