Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Multidimensional list in python...

I cannot seem to loop through printing out the values of the grid in python using a multidimensional list.

Here is my code;

grid = [
        ['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

for i in range(0,len(grid),1):
    for j in range(0,len(grid[i]),1):
        if j == (i + 5):
            print(grid[i][j],end=' ')

At the moment the result I get is this;

. . . . . .
. O O . . . O O O O . . O O O O O . . O O O O O O O O O O . O O O O . . . O O . . . . . . . . .

It seems to print out the top line fine but not the others!


Manage to sort it out. Was getting confused how to print a new line;

I needed to be using a print() after my inner for loop like so.

for i in range(len(grid)):
    for j in range(len(grid[i])):
        print(grid[i][j],end= ' ')

Yes I think I get what you are doing, thanks. I'm gonna look at it again, just cause I'm fascinated.

1 Answer

Hi Mayur, I'm not really sure what you are trying to accomplish, but I was fascinated by your code. I probably can't be real helpful, but here's what I noticed. You said it only prints the top line, but it looks to me like all the information is there...but it's on one line. I'm wondering if it's your use of end="", which is intended to put things all on one line. I hope you post when you have this doing what you intended, I'd like to see haw it works.

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Hi John,

I want to loop through printing each coordinate like a multidimensional array in js. For example;

0,0 | 0,1 | 0,2
1,0, | 1,1 | 1,2 

But instead of the location the value at those points. Hope this makes more sense?