Python Forum
No matter what I do I get back "List indices must be integers or slices, not list" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: No matter what I do I get back "List indices must be integers or slices, not list" (/thread-40785.html)



No matter what I do I get back "List indices must be integers or slices, not list" - Radical - Sep-23-2023

EDIT: Solved

I'm having an issue with my code, and Google seems to think it's a simple fix, but I've been trying the fixes and they aren't working. I don't understand why it doesn't work. (The goal of the code is to loop through grid, printing each string value in the list, then going to the next line and printing each value in the next list, etc. in order to form a picture.)

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 y in grid:
    for x in grid[y]:
        print(grid[x][y], end = '')
    print('')
This gives the error "list indices must be integers or slices, not list". But I've tried other things such as:
for x in len(grid[y]):
for x in int(grid[y]):
for x in int(len(grid[y]):
for x in grid[int(y)]):
I really don't know what else to do. I must be missing something simple. I realize I could easily just write:
for x in grid[9]:
but this ruins scalability, and I still run into the same error on the line:
print(grid[x][y])



RE: No matter what I do I get back "List indices must be integers or slices, not list" - Gribouillis - Sep-23-2023

Try to run
for y in grid:
    print(y)
Do you think y is an integer?
But if it's not an integer, one cannot write grid[y] ! Compare with your code.


RE: No matter what I do I get back "List indices must be integers or slices, not list" - Radical - Sep-23-2023

It returns what I expected. "y" is a list with 6 strings inside. I figured that it would loop through the items in "y" the same way it looped through the different lists in "grid".

UPDATE: Wait..... you're right. Okay I got my code to work. The solution was:
for y in grid:
    for x in y:
        print(x, end = '')
    print('')
Thank you :3


RE: No matter what I do I get back "List indices must be integers or slices, not list" - Gribouillis - Sep-23-2023

(Sep-23-2023, 06:04 PM)Radical Wrote: print('')
You can just write print() instead.


RE: No matter what I do I get back "List indices must be integers or slices, not list" - deanhystad - Sep-24-2023

Quote:It returns what I expected. "y" is a list with 6 strings inside. I figured that it would loop through the items in "y" the same way it looped through the different lists in "grid".
That is exactly what it does. y iterates through the rows in grid, and x iterates through the strings in y.
for y in grid:
    for x in y:
        print(x, end=' ')
    print()
Mixing up iterating and indexing is the exact same problem you had here:

https://python-forum.io/thread-40750.html

I know you are probably trying to learn how to do loops, but nobody would print out your grid using two loops. They might do this:
for row in grid:
    print(*row, sep=" ")
But it's more likely they would do this:
for row in grid:
    print(" ".join(row))
Or this.
print("\n".join(" ".join(row) for row in grid))