Python Forum

Full Version: No arrays in Python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm a complete Python novice, although I've written a million lines of 360 Assembler (40 - 50 years ago!). I'm trying to emulate 2-dimensional arrays using Lists. Apparently, I have some basic misunderstanding. I create a 3x3 array and then fill the cells with a simple sequence number (ie, 1 - 9). As I do this, the number properly increments from 1 to 9. However, when I go back and print each cell again, the contents have been altered. What am I missing!

Here is the code (output then follows at the end).

# create a two dimensional array

# create empty grid lists
row =  []
col =  []
grid = []

# create grid with dimx rows and dimy columns
dimx = 3
dimy = 3
for y in range(0,dimy):
    col.append(y)
for x in range(0,dimx):
    grid.append(col)

# fill cells with a consecutive number (as a test)
cntr = 1
y = 0
while y < dimy:
    x = 0
    while x < dimx:
        grid[x][y] = cntr
        cntr = cntr + 1
        print (x,y,grid[x][y])
        x = x + 1
    y = y + 1

# now go back print contents of each cell
y = 0
while y < dimy:
    x = 0
    while x < dimx:
        print (x,y,grid[x][y])
        x = x + 1
    y = y + 1
---------------------------------- output follows -------------
Output:
0 0 1 1 0 2 2 0 3 0 1 4 1 1 5 2 1 6 0 2 7 1 2 8 2 2 9 0 0 3 1 0 3 2 0 3 0 1 6 1 1 6 2 1 6 0 2 9 1 2 9 2 2 9
Lists stores indexes as memory pointer. Since the script appends the same list to grid three times, each update on line 22 changes the same list. When it prints out, it will print the last value assigned to that index.
There are two things to note here:
First, there are arrays in Python, but what you are using here are lists. You can get access to arrays through the numpy package. Lists accept every datatype possible. An array on the other hand just accepts one datatype.

Second, if you construct lists in python and pass these lists anywhere you pass a reference to the original list. so for example
a = [1, 2, 3, 4]
b = a
a[0] == 6
print(a)
print(b)
Output:
[6, 2, 3, 4] [6, 2, 3, 4]
So in your case you are appending the col array 3 times to the grid. Now when you change one value it will be changed in every row of the grid. for example:
a = [1, 2, 3]
grid = [a, a, a]
print(grid)
grid[0] = 6
print(grid)
Output:
[[1, 2, 3], [1, 2, 3], [1, 2, 3]] [[6, 2, 3], [6, 2, 3], [6, 2, 3]]
Now take a look at your output. you are filling your list like this:
Output:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
The last values you assign to the referenced col list are 3, 6 and 9. So that your list now looks like this:
Output:
[[3, 6, 9], [3, 6, 9], [3, 6, 9]]
If you want to build a list out of an existing list this is what you could do
dimx = 3
dimy = 3
for y in range(0,dimy):
    col.append(y)
for x in range(0,dimx):
    grid.append(col.copy())
note that <somelist>.copy() does return a copy of your list just like list(<somelist>)
And one last tip, don't confuse the x and y coordinates, your first dimension you pass to the list should be the y coordinate and the second one the x coordinate