Home > Enterprise >  Why do I get "pop index out of range" error when running this?
Why do I get "pop index out of range" error when running this?

Time:07-17

I am writing code to make a bot for the math game "Dandelions" and in order to do so, I take all columns, rows, and diagonals that contain the number 1. Then, with this list of lists that contain the number 1, I need to separate them using a for loop. However, when running this for loop I get an error saying that the pop index is out of range, but when I run the code with each individual i value, I don't get an error. It is the last for loop at the very end of the code that is causing this problem. Please help! Note: Even when running the for loop with a range of 3, it still outputs an error. Only indexes 0 and 2 are outputted.

row_1 = [0, 0, 0, 0, 0]
row_2 = [0, 1, 0, 0, 0]
row_3 = [0, 0, 0, 0, 0]
row_4 = [0, 0, 0, 0, 0]
row_5 = [0, 0, 0, 0, 0]

col_1 = [0, 0, 0, 0, 0]
col_2 = [0, 0, 1, 0, 0]
col_3 = [0, 0, 0, 0, 0]
col_4 = [0, 0, 0, 0, 0]
col_5 = [0, 0, 0, 0, 0]

#diagonals 1-9 go from left to right, while diagonals 10-18 go from right to left

dia_1 = [0]
dia_2 = [0, 0]
dia_3 = [0, 0, 0]
dia_4 = [0, 0, 0, 0]
dia_5 = [0, 0, 0, 1, 0]
dia_6 = [0, 0, 0, 0,]
dia_7 = [0, 0, 0]
dia_8 = [0, 0]
dia_9 = [0]

dia_10 = [0]
dia_11 = [0, 0]
dia_12 = [0, 0, 0]
dia_13 = [0, 0, 0, 0]
dia_14 = [0, 0, 0, 0, 1]
dia_15 = [0, 0, 0, 0,]
dia_16 = [0, 0, 0]
dia_17 = [0, 0]
dia_18 = [0]

dia = [dia_1, dia_2, dia_3, dia_4, dia_5, dia_6,
       dia_7, dia_8, dia_9, dia_10, dia_11, dia_12,
       dia_13, dia_14, dia_15, dia_16, dia_17, dia_18]
row = [row_1, row_2, row_3, row_4, row_5]
col = [col_1, col_2, col_3, col_4, col_5]
possible = []

for i in dia:
    if 1 in i:
        possible.append(i)

for i in row:
    if 1 in i:
        possible.append(i)

for i in col:
    if 1 in i:
        possible.append(i)

for i in range(0, 4):
    print(possible.pop(i))

CodePudding user response:

The for loops that are giving you the out-of-range error wrap them on a try-except block, in the exception add new logic to ignore or fix the out of range; for instance:

try:
    for i in range(0, 4):
        print(possible.pop(i))
except:
    #correct your out-of-range:
    pass

CodePudding user response:

Seems you did not get my hint and the only answer so far is not very helpful.

To reproduce the problem, let's hard-code possible, since everything before the last loop does not really matter.

possible = [[0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]
print(f"Possible before loop: {possible}")
for i in range(0, 4):
    print(f"Item at pos {i}: {possible.pop(i)}")
    print(f"Possible after pop: {possible}")

Output:

Possible before loop: [[0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]
Item at pos 0: [0, 0, 0, 1, 0]
Possible after pop: [[0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]
Item at pos 1: [0, 1, 0, 0, 0]
Possible after pop: [[0, 0, 0, 0, 1], [0, 0, 1, 0, 0]]
Traceback (most recent call last):
  File "test.py", line 5, in <module>     
    print(f"Item at pos {i}: {possible.pop(i)}")
IndexError: pop index out of range

Explanation:

As I said before, i keeps increasing, but at the same time, you are removing items from possible.

After the first iteration possible is [[0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]].

But during the next (the second) iteration, you are popping the item at index 1, which is [0, 1, 0, 0, 0] - you were probably expecting [0, 0, 0, 0, 1].

During the third iteration, possible only holds two items: [[0, 0, 0, 0, 1], [0, 0, 1, 0, 0]] but you are trying to pop the item at index 2, which is when the pop index out of range error happens.

Why not simple iterate your list and print the items instead of popping them?

possible = [[0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]
for item in possible:
    print(item)

Output:

[0, 0, 0, 1, 0]
[0, 0, 0, 0, 1]
[0, 1, 0, 0, 0]
[0, 0, 1, 0, 0]
  • Related