Skip to content Skip to sidebar Skip to footer

Indexerror: Pop From Empty List

I need help. I have no idea why I am getting this error. The error is in fname = 1st.pop() for i in range(num) : fname = lst.pop() lTransfer = [(os.path.join(src,

Solution 1:

lst has less elements than num

use

for i inrange(min(num, len(lst))):`

or something like

for fname inreversed(lst):# reversed to continue the pop order#your code

Explanation

#lets say we have
num = 4
data = [1,2,3]

for i in range(num): # range(4) = [0,1,2,3] so it witl repeat you code 4 times
    data.pop() #remove last element#first 3 times, it works, but at the last one 'data' is empty, so you get an exception

if you do:

for i inrange(min(num , len(data))):
# min(num , len(data)) = min(4,3) = 3# so you get the corrent number of iterations

Finally:

for fname inreversed(data):
#is the same tofor fname in [3,2,1]:
#'reversed' just change the order of your list#so it will work in this order, 3, 2 and finishes with 1

Hope it helps

Post a Comment for "Indexerror: Pop From Empty List"