Python: IndexError: list index out of range and Class objects

enter image description here

I hope to 2-D list. And each element in the list is set to unique class object. But I get the error(please see the screenshot). How can I get it done? Thank you!

2 answers

  • answered 2018-05-16 05:34 Vikas Damodar

    Here before you assigning values to 2-d array Vertion you have to create an empty list with the exact number of rows and columns you want. Then only you can assign values to each each columns in each rows. Here you can take a look :

    Vertion = [[0 for x in range(128)] for y in range(2)] 
    

    Update :

    So for assign values to columns in each rows you can try like this :

    for i in range(2):
        for j in range(128):
            Vertion[i][j] = Block()
    

  • answered 2018-05-16 05:39 Kei H

    Vertion = [[], []]
    

    as Vertion is a list with two empty list, so the for loop should be like this

    for i in range(len(Vertion)):
        for j in range(128):
            Vertion[i].append(Block())