[Tutor] Generate array, different results

Pavel Andreev apn06y at gmail.com
Fri Sep 21 20:47:47 CEST 2012

Hello all
A question from very beginner.
I generate a simple example array and then print its elements on the screen.

f=[[0]*3]*3                      # array initialization
for i in range(3):
    for j in range(3):
        f[i][j]=str(i)+str(j)    # make simple example array
        print f[i][j],           # print resulting elements
    print '\n'

 00   01   02

 10   11   12

 20   21   22

So I generate element in loop and then print it and see all is ok.

But after that if I print the whole generated array, I see different
result with constant first number, as if "i" variable does not vary
and equals to "2". What is wrong?

>>> print f

[['20', '21', '22'], ['20', '21', '22'], ['20', '21', '22']]

I use python 2.7

More information about the Tutor mailing list