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


Messages In This Thread
No matter what I do I get back "List indices must be integers or slices, not list" - by Radical - Sep-23-2023, 05:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 3 390 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  tuple indices must be integers or slices, not str cybertooth 16 11,998 Nov-02-2023, 01:20 PM
Last Post: brewer32
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,375 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,436 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Delete strings from a list to create a new only number list Dvdscot 8 1,659 May-01-2023, 09:06 PM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,600 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,542 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  List all possibilities of a nested-list by flattened lists sparkt 1 972 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,129 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,532 Dec-30-2022, 05:38 PM
Last Post: dee

Forum Jump:

User Panel Messages

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