0

I'm trying to run a while loop to remove items from a list until the loop stops removing items. I'm doing this by setting a 'lastlist' variable every time the loop is run and then checking if the list equals the lastlist before running the loop. The loop only runs once and I don't understand why.

I can setup an equivalent loop with an integer, where I compare the integer to the last integer and it runs as many times as I would expect. What am I not understanding about lists vs. numbers in while loops?

list = [0,1,2,3,4,5]
lastlist = []
while list != lastlist:
    lastlist = list
    del list[-1]
print(list)
#prints [0,1,2,3,4]

var = 5
lastvar = 0
while var != lastvar:
    lastvar = var
    if var > 0:
        var -= 1
print(var)
#prints 0

0 Answers0