3

I want to create a 2D grid with for loop in python. I can do simple code like this:

cols = 10
rows = 10
grid = [[0 for x in range(cols)] for y in range(rows)]
print(grid)

But when I try to loop through i in rows and then j in columns, it shows error: list index out of range. Not sure where went wrong with my coding?

rows = 10
cols = 10
i = 0
for i in range(rows):
    for j in range(cols):
        grid[i].append([j])
        i += 1
4
  • 1
    [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] I tried to use for loop to get the same result as the first one does. Commented Oct 30, 2018 at 17:59
  • Why are you trying to append more lists to the sublists of grid? Why are you incrementing i inside the inner loop? Commented Oct 30, 2018 at 18:00
  • If you are trying to recreate the output of the first code then you could start with grid defined as an empty list. Commented Oct 30, 2018 at 18:04
  • When you get to a resolution, please remember to up-vote useful things and accept your favourite answer (even if you have to write it yourself), so Stack Overflow can properly archive the question. Commented Oct 30, 2018 at 18:16

4 Answers 4

3

You need to create an empty sublist before you use something like grid[i].append(). Because initially there is nothing in the list and you refer to something that is not available. Hence, your error. :(

You could instead create a sublist in each outer iteration and append 0 to previous sublist in the inner iteration:

cols = 10
rows = 10

grid = []
for _ in range(rows):
    grid.append([])
    for _ in range(cols):
        grid[-1].append(0)

print(grid)

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


Combining the whole to one line:

grid = [[0 for _ in range(cols)] for _ in range(rows)]
Sign up to request clarification or add additional context in comments.

2 Comments

This is cool! But I am not sure i understand how does the _ work? and why the row start from -1?
@user10381476, _ is just like a throw away variable. You don't need the iterator, so a _ is just enough. -1 will get the last sublist of list so you could append to that last sublist.
2

There a few issues:

  1. You don't instantiate an empty grid list prior to your loop.
  2. You have to append sublists to your outer list before trying to append to those sublists.
  3. When iterating over a range, there's no need to increment the counter.

So this will work:

rows = 5
cols = 5

grid = []
for i in range(rows):
    grid.append([])
    for j in range(cols):
        grid[i].append(0)

print(grid)

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

Comments

2

You are incrementing i twice. First time in for i in range(rows), second time with i += 1. Remove second statement so it will look like this:

cols = 10
rows = 10
grid = [[0 for x in range(cols)] for y in range(rows)]

for i in range(rows):
    for j in range(cols):
        grid[i].append([j])

instead of:

for i in range(rows):
    for j in range(cols):
        grid[i].append([j])
        i += 1

Also this variable initialization is of course unnecesary: i = 0


If you want to find alternative way to do that:

grid = [[0 for x in range(cols)] for y in range(rows)]

You have to also initialize your array with nested array:

grid = []
for i in range(rows):
    grid.append([])
    for j in range(cols):
        grid[i].append(0)

1 Comment

Thanks, very helpful!
1

Your problem is in the increment in your inner loop:

for i in range(rows):
    for j in range(cols):
        grid[i].append([j])
        i += 1          # BAD!!

The inner loop increments i 10 times, driving it out of range. Get rid of that statement, and you might get what you want. Given the strange programming style, I'm not quite sure what your construction is supposed to do. Without the superfluous increment, you're appending single-element lists to your original ten zeros:

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]]

Did you want the elements to be simply 0-9? For that:

grid = [list(range(10)) for _ in range(10)]

[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

1 Comment

This is good enough. I just need a text-based game board. Numbers do not matter. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.