How do I make a grid in python?

Question:

This is my code

width = int(input("How wide?"))
height = int(input("How high?"))
grid = []
row = []
bak = "."
for i in range(width):
    row.append(bak)
for i in range(height):
    grid.append(row)
while True:
    for i in range(len(grid)):
        print(grid[i])

It’s not working and i don’t know why. This is what i get when i put 5 width and 5 height:

['.', '.', '.', '.', '.']
['.', '.', '.', '.', '.']
['.', '.', '.', '.', '.']
['.', '.', '.', '.', '.']
['.', '.', '.', '.', '.']

That’s fine and all but when i change the bottom left dot by using this: grid[0][0] = "a".
This happens:

['a', '.', '.', '.', '.']
['a', '.', '.', '.', '.']
['a', '.', '.', '.', '.']
['a', '.', '.', '.', '.']
['a', '.', '.', '.', '.']

It thinks the "row" list is a tag, when it is clearly coded not to be.
How to fix this problem?

Asked By: SollyBunny

||

Answers:

for i in range(height):
    grid.append(row)

This for loop appends the same row list to the grid list (actually it appends 5 different references to the same list).

Instead, you should append a new, different list:

for i in range(height):
    grid.append([])

Verify by viewing the memory addresses of the inner lists in these 2 examples:

grid = []
row = []
for i in range(5):
    grid.append(row)
for li in grid:
    print(id(li))
# 92532104
# 92532104
# 92532104
# 92532104
# 92532104

compared to

grid = []
for i in range(5):
    grid.append([])
for li in grid:
    print(id(li))

# 80801224
# 80801160
# 80669704
# 80381192
# 80380488
Answered By: DeepSpace

Use list()

gridline = []
for i in range(5):
    gridline.append("")
grid = []
for i in range(5):
    grid.append(list(gridline))
Answered By: SollyBunny

Just use this.

    width = int(input("how wide "))
    height = int(input("how tall "))
    grid = []
    i = int(0)
    for i in range(width):
        grid.append("_")
    for i in range(height):
        print(grid)
Answered By: Trigger-Me-Elmo
# creating grid
length = int(input("enter your grid length"))
breath = int(input("enter your grid breath"))
board = []
for i in range(length):
   board.append(["*"] * breath)
for x in board:
   grid = (" ".join(x))
   print(grid)
Answered By: user16347377

Your code code contains too many unnecessary variables for just a simple grid.

You can use list comprehension to simplify your code which can help you debug:

grid = [['.' for i in range(width)] for i in range (height)]

To print the grid you can do the following:

for row in grid:
    print(*row, sep=" ") 

The * symbol is to only print the values in the grid (While removing pointless commas and brackets).

The "sep" parameter tells the print function to seperate each value with a string or character. In this case, it’s a space.

If you have any more questions regarding this code I’m happy to help!

Answered By: Aidan Pastor
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.