By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

A Q. on pop().

P: n/a
Hi,
The code.

def buildStackMajor():
for node in dirStackMinor:
#print 's is the node...', node
dirStackMajor.append(node)
dirStackMinor.pop()
print 'POP the stack...', len(dirStackMinor)
print 'after pop...', dirStackMinor

When I start the "for" loop I have 11 members in
the stack- a list. The pop() is poping member
from the back, as though it's deal with a LIFO
stack instead of LIFO. I never get pass a length
of 5.
first Q.. Why is pop() starting from the back
back of the stack?
second Q.. Why can't I never empty the stack?
Thanks.

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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[:]
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.