Subhan Sheikh
Subhan Sheikh

Reputation: 11

Getting IndexError: list index out of range, does not work in if/else stement

Hi I'm running this code in Anaconda Python 3.9. Problem is it orints just fine in the for loop all the elemtns, but in the if and else statement it does not. It printed 1.422 85 which is correct (means it also accesed the last element) then procceded to give the error "IndexError: list index out of range" for my else statement. When right beofre it. printed the same array with almost identical code

How do i fix this ?

sr = [(5001, 1.17135, 2), (5003, 1.14463, 1), (5028, 1.24852, 2), (5031, 1.23169, 1), (5039, 1.24726, 2), (5050, 1.2475, 2), (5065, 1.21645, 1), (5068, 1.23839, 2), (5082, 1.24832, 2), (5100, 1.26471, 2), (5104, 1.24362, 1), (5122, 1.2416, 1), (5129, 1.2247, 1), (5151, 1.24543, 2), (5171, 1.26426, 2), (5182, 1.2405, 1), (5221, 1.22509, 1), (5224, 1.23396, 2), (5230, 1.21654, 1), (5261, 1.22493, 2), (5279, 1.23628, 2), (5285, 1.2204, 1), (5290, 1.22335, 1), (5335, 1.27552, 2), (5354, 1.25449, 1), (5367, 1.26872, 2), (5385, 1.24557, 2), (5389, 1.2335, 1), (5401, 1.25425, 2), (5408, 1.24037, 1), (5423, 1.22509, 1), (5440, 1.25296, 2), (5471, 1.26693, 2), (5495, 1.26492, 2), (5550, 1.29521, 2), (5556, 1.3013, 2), (5567, 1.31699, 2), (5573, 1.30044, 1), (5577, 1.31078, 2), (5597, 1.3009, 1), (5603, 1.31029, 2), (5614, 1.30047, 1), (5621, 1.31246, 2), (5631, 1.31213, 2), (5655, 1.32552, 2), (5657, 1.30588, 1), (5674, 1.31168, 1), (5698, 1.3482, 2), (5711, 1.32419, 1), (5736, 1.30226, 2), (5742, 1.27725, 1), (5746, 1.2865, 2), (5753, 1.29189, 2), (5756, 1.28146, 1), (5781, 1.2966, 2), (5788, 1.28666, 2), (5812, 1.29296, 2), (5819, 1.29024, 2), (5830, 1.29784, 2), (5847, 1.30067, 2), (5859, 1.29699, 2), (5872, 1.30051, 1), (5891, 1.289, 1), (5896, 1.28562, 1), (5903, 1.30241, 2), (5908, 1.29105, 1), (5908, 1.29793, 2), (5915, 1.31764, 2), (5939, 1.30794, 2), (5951, 1.28804, 1), (5993, 1.31182, 1), (5999, 1.32783, 2), (6005, 1.31909, 1), (6021, 1.32426, 2), (6064, 1.33038, 1), (6078, 1.32877, 1), (6083, 1.33845, 2), (6101, 1.35001, 2), (6107, 1.35397, 2), (6154, 1.35538, 2), (6161, 1.36245, 2), (6172, 1.31877, 1), (6190, 1.36193, 2), (6197, 1.34293, 1), (6214, 1.36863, 2), (6220, 1.37037, 2), (6221, 1.35418, 1), (6245, 1.36356, 2), (6261, 1.37008, 2), (6263, 1.36117, 1), (6279, 1.35196, 1), (6292, 1.37186, 2), (6304, 1.36356, 1), (6330, 1.37455, 2), (6333, 1.36573, 1), (6339, 1.37579, 2), (6345, 1.37105, 2), (6347, 1.36113, 1), (6350, 1.36831, 2), (6393, 1.37756, 1), (6413, 1.38296, 1), (6454, 1.38877, 1), (6466, 1.38588, 1), (6468, 1.39769, 2), (6479, 1.40165, 2), (6501, 1.38449, 1), (6511, 1.40046, 2), (6515, 1.38628, 1), (6550, 1.38763, 2), (6587, 1.37057, 1), (6593, 1.38115, 2), (6597, 1.37458, 1), (6603, 1.38514, 2), (6624, 1.37238, 1), (6658, 1.38085, 2), (6663, 1.37157, 1), (6675, 1.40087, 2), (6690, 1.38238, 1), (6699, 1.39291, 2), (6707, 1.3924, 2), (6712, 1.38611, 1), (6716, 1.39767, 2), (6731, 1.39319, 2), (6736, 1.38382, 1), (6767, 1.41662, 2), (6796, 1.422, 2), (6819, 1.41116, 1), (6825, 1.42109, 2), (6832, 1.41759, 2), (6836, 1.40914, 1), (6840, 1.42193, 2), (6879, 1.41115, 1), (6882, 1.41911, 2), (6897, 1.40733, 1), (6903, 1.41853, 2), (6916, 1.40344, 1), (6922, 1.41327, 2), (6929, 1.38959, 1), (6930, 1.38959, 1), (6942, 1.3937, 2), (6946, 1.38603, 1), (6953, 1.40013, 2), (6976, 1.3814, 1), (6996, 1.38445, 2), (7005, 1.3898, 2), (7007, 1.3773, 1), (7012, 1.38418, 2), (7018, 1.37422, 1), (7028, 1.39099, 2), (7053, 1.38618, 2), (7067, 1.35719, 1), (7117, 1.38839, 1), (7126, 1.39389, 2), (7139, 1.39489, 2), (7170, 1.3795, 1), (7176, 1.38751, 2), (7194, 1.37865, 2), (7212, 1.37321, 2), (7222, 1.36968, 1), (7225, 1.37681, 2), (7231, 1.36797, 1), (7232, 1.36797, 1), (7235, 1.37803, 2), (7251, 1.37304, 1), (7253, 1.37976, 2), (7261, 1.38453, 2), (7266, 1.38914, 2), (7294, 1.38884, 2), (7307, 1.39131, 2), (7310, 1.3792, 1), (7319, 1.37642, 1), (7332, 1.36406, 1), (7344, 1.36092, 1), (7345, 1.36092, 1), (7349, 1.37505, 2), (7361, 1.37287, 2), (7368, 1.35205, 1), (7373, 1.34117, 1), (7389, 1.35316, 1), (7391, 1.364, 2), (7397, 1.36478, 2), (7402, 1.35438, 1), (7409, 1.3638, 2), (7413, 1.35835, 1), (7426, 1.36374, 2), (7438, 1.37343, 2), (7456, 1.3834, 2), (7474, 1.38148, 2), (7486, 1.38295, 2), (7517, 1.36051, 1), (7525, 1.36979, 2), (7534, 1.34242, 1), (7539, 1.34494, 1), (7541, 1.3579, 2)]
plotlist1 = [x[1] for x in sr if x[2]==1] # stores values support 
plotlist2 = [x[1] for x in sr if x[2]==2] # stores values resistance 
plotlist1.sort() # sorts supports 
plotlist2.sort() # sorts resistance

pipdif = 0.0005

print (len(plotlist2))
print (plotlist2)
for i in range(1,len(plotlist2)): # starting from 1 to len of resitance val
    print (plotlist2[i], (i+1))
    if(i>=len(plotlist2)): #once done break 
        break
    if abs(plotlist2[i]-plotlist2[i-1])<=pipdif: # if abs val of current - [previous] is less or equal then pipdif
        pop = plotlist2.pop(i) # delete curent one
        print ("zone is bad", plotlist2[i],(i+1))
        #print (plotlist2[i], (i+1), "-",plotlist2[i-1],(i), "=", abs(plotlist2[i]-plotlist2[i-1]), "Deleting", (plotlist2[i]),(i+1))
    else:
        print ("zone is valid", plotlist2[i],(i+1))

OUT PUT recieved

zone is valid 1.42193 84
1.422 85
Traceback (most recent call last):
  File "/Users/Subhan109/Documents/Untitled Folder/test index.py", line 16, in <module>
    print ("zone is bad", plotlist2[i],(i+1))
IndexError: list index out of range

Upvotes: 1

Views: 109

Answers (2)

Subhan Sheikh
Subhan Sheikh

Reputation: 11

Edit: I fixed it the order of the print and pop needed to be reversed

for i in range(1,len(plotlist2)): # starting from 1 to len of resitance val
if(i>=len(plotlist2)): #once done break 
    break
if abs(plotlist2[i]-plotlist2[i-1])<=pipdif: # if abs val of current - [previous] is less or equal then pipdif
    print ("zone is bad", plotlist2[i],(i+1), plotlist2[i], (i+1), "-",plotlist2[i-1],(i), "=", abs(plotlist2[i]-plotlist2[i-1]) ,"<", pipdif,(abs(plotlist2[i]-plotlist2[i-1])<=pipdif) ,"Deleting", (plotlist2[i]),(i+1))
    pop = plotlist2.pop(i) # delete curent one
else:
    print ("zone is valid", plotlist2[i],(i+1))

Upvotes: 0

Seliksander
Seliksander

Reputation: 54

On the line before the error occours, you call plotlist2.pop(i). This will reduce the length of the list by 1, and yet after each call, the for loop will increase i by 1.

In practice, this means that you are iterating over only every other element (skipping over odd-indexed elements). As this will be performed an amount of times equal to the length of the unpruned list, you will quickly attempt to access an invalid index.

Instead of removing elements from the list, consider constructing a secondary list with only the elements that would not have been removed. This avoids the problem of removing elements while iterating index-wise.

Edit

I'm unable to comment on @Ayush's answer (as I do not have the reputation requirement), but their solution will not work, as i will still exceed the length of the pruned list.

Upvotes: 1

Related Questions