Home > Back-end >  what is 'list index out of range'
what is 'list index out of range'

Time:12-29

for this code:

file = r<path>
import fitz
sentence = ""
count_block = 0
count_line = 0
count_element = 0
count_element_1 = 0
word_info = []
data = []
p_num = []
# lists of word information
wList = []
bList = []
lList = []
wnList = []
sList = []
# Word Coordinates
word = 4
block = 5
line = 6
word_number = 7
#
doc = fitz.open(file)
pages = len(doc)
count_1 = 0

for page in range(pages):
    print(f'page:{page}')
    for element in list(doc[page].get_text("words")):
        print(element)
        if element[4] == 'Features' or element[4].lower == 'product':
            for element_1 in list(doc[page].get_text("words")):
                if count_element_1 - 1 >= count_element:
                    wList.append(element_1[word])
                    bList.append(element_1[block])
                    lList.append(element_1[line])
                count_element_1  = 1
        count_element  = 1
        count_element_1 = 0
    count_element = 0
    for a, b, c in zip(wList, bList, lList):
        word_info.append([a, b, c])
    print(word_info)

when I try print(word_info) I get:

[['Model', 4, 0], ['No.', 4, 0], ['KX', 4, 1], ['1504.000', 4, 1], ['Product', 5, 0], ['description', 5, 0], ['The', 5, 1], ['small', 5, 1]]

but when I try print(word_info[0][0]) I get:

error list index out of range

note: this what the element looks like>>(88.046875, 562.0, 222.33563232421875, 599.5, '1504.000', 0, 0, 1)

CodePudding user response:

The IndexError: list index out of range error occurs in Python when an item from a list is attempted to be accessed that is outside the index range of the list. You can also find your answer here on this link

CodePudding user response:

A Python list of length N where N > 0 can be subscripted by indexes in the the range -N to N-1

Any attempt to subscript a list outside of these limits will induce IndexError.

  • Related