Python Forum
No matter what I do I get back "List indices must be integers or slices, not list"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No matter what I do I get back "List indices must be integers or slices, not list"
#1
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])
Reply
#2
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.
Radical likes this post
Reply
#3
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
Reply
#4
(Sep-23-2023, 06:04 PM)Radical Wrote: print('')
You can just write print() instead.
Radical likes this post
Reply
#5
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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,622 Nov-02-2023, 01:20 PM
Last Post: brewer32
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,280 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,358 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Delete strings from a list to create a new only number list Dvdscot 8 1,556 May-01-2023, 09:06 PM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,444 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,457 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  List all possibilities of a nested-list by flattened lists sparkt 1 929 Feb-23-2023, 02:21 PM
Last Post: sparkt
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,051 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,474 Dec-30-2022, 05:38 PM
Last Post: dee
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,859 Oct-26-2022, 04:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020