A Q. on pop().

Duncan Booth duncan.booth at invalid.invalid
Thu Mar 17 17:54:58 CET 2005


spencer wrote:

> first Q.. Why is pop() starting from the back
>           back of the stack?

Because that is what it does. Try reading the documentation:

>>> help(list.pop)
Help on method_descriptor:

pop(...)
    L.pop([index]) -> item -- remove and return item at index (default 
last)


> second Q.. Why can't I never empty the stack?

Because you are modifying a list while iterating over it which is never a 
good idea. What you have now pops items from the end of the loop so it 
stops about half way along. If you change it to pop item 0 then it will 
shift the items down and your iteration will end up skipping over about 
half of them.

Try something like this:

def buildStackMajor():
    while dirStackMinor:
        dirStackMajor.append(dirStackMinor.pop(0))

although in that case you might just as well get rid of the loop entirely:

    dirStackMajor += dirStackMinor
    del dirStackMinor[:]



More information about the Python-list mailing list