pythonmultidimensional-arrayarraylistindex-error

Python: 'IndexError: list assignment index out of range' from 2d-List


I got the error

IndexError: list assignment index out of range

by using the following code:

expC = 0
imgC = 0
pictures = [[x for x in range(7)] for y in range(25)]
for i in experimentNames:
    for pictureData in glob.iglob('{}/**/*.png'.format(i), recursive=True):
        img = cv.imread(pictureData)
        pictures[expC][imgC] = img
        imgC = imgC + 1
    expC = expC + 1

The goal of this code is to write pictures in a 2 dimensional list. Each column should represent one experiment. There are seven experiments with 25 pictures. They should be saved in the matching column.

I tried the code above and changed the indexes and the declaration of the matrix. Still didn't work.


Solution

  • I found the error. I have more than 25 images so the range of the list is too small. Moreover I forgot resetting imgC.