0

I want to create a grid with a variable number of rows and columns. What I have done to achieve this is this

BaseRow = []
for j in range (0, columns):
    BaseRow.append(0)
Grid = []
for j in range (0, rows):
    Grid.append(BaseRow)

So all seems fine until now, I print the rows in order with this piece of code

for i in range (1, rows+1):
    print Grid[rows-i]

and a grid that looks something like this

[0, 0, 0]
[0, 0, 0]
[0, 0, 0]

is printed. Thing is, afterwards, I want to change a specific element. But when I do,

Grid[0][0] = 1

and print again, instead of just changing the rightmost down most 0 to a 1, the whole column changes, so it becomes

[1, 0, 0]
[1, 0, 0]
[1, 0, 0]

I suspect it sees that Grid is taking from BaseRow, so it changes BaseRow, and then the Grid takes the rows from BaseRow and just puts that value everywhere. I suspect .append might not be what I am looking for, but for all the research I have done I have not managed to find something else to use. If I understand correctly, .extend will not add it as a list but as individual numbers. What should I change, or how should I structure this?

Please excuse my limited knowledge, I just started programming in python half a week ago. Thanks for your help!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

1
BaseRow = []
for j in range (0, columns):
    BaseRow.append(0)
Grid = []
for j in range (0, rows):
    Grid.append(BaseRow)

When you do this, the same instance of BaseRow is appended to Grid multiple times. So, if you change even row in Grid, the effect will be on all rows, as it is basically the same instance of list in all rows.

If you want a copy of BaseRow to be appended to Grid, use the below code:

for j in range(0, rows):
    Grid.append(BaseRow[:])

You could also use list comprehension:

Grid = [[0 for j in range(0, columns)] for i in range(0, rows)]

Output for Columns = 3 and rows = 4:

[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

Output after setting Grid[0][0] = 1:

[[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jay
  • 24,173
  • 25
  • 93
  • 141
  • Is there a way to append a different instance of the BaseRow to the Grid? Or some way to create a variable number of BaseRow lists so that I can append a different one to each element of the Grid? – Skilopsaros Feb 04 '19 at 12:35
0

If you ask me, I would any day use List comprehension because it's so clean and easy:

columns, rows = 3, 3

lst = [[0 for j in range(columns)] for i in range(rows)] # List of List with 3 Columns and 3 Rows
lst[0][0] = 1                                            # modifying a member
print (lst)                                              # Print the result

# Result: [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Akash Swain
  • 520
  • 3
  • 13
0

I personally prefer list comprehension but your code needs just little changes and it will serve you well. You append list and to that list you append elements:

matrix = []
for i in range(3):
    matrix.append([])
        for j in range(4):
            matrix[-1].append(0)

print(matrix)
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
matrix[0][0] = 1
print(matrix)
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Aivar Paalberg
  • 4,645
  • 4
  • 16
  • 17