Dynamically growing an array to implement a stack

Patrick Maupin pmaupin at gmail.com
Thu Apr 8 20:15:55 EDT 2010

On Apr 8, 3:54 pm, "M. Hamed" <mhels... at hotmail.com> wrote:
> Thanks Patrick, that is what I was exactly looking for.

You're welcome!

But I have to say, you should consider what Paul and Lie are saying.
In general, when I use a stack, I just use append() and pop(), as they
mention, and let the list automagically keep track of the pointer.  (I
don't usually even bother subclassing list, but I will often write
something like push = mylist.append; pop = mylist.pop)

But since I don't know about your specific problem, and you obviously
didn't know you could assign to a slice of a list in this manner, I
focused on what you were asking for.

Paul and Lie are trying to focus more on what you really need than
what you asked for, and since you mentioned the magic word "stack",
they immediately assumed that you meant a standard LIFO, where you
only do operations on a single element at a time on one end of the
list.  That's a fair assumption, and if that's the case, you should
consider whether you even need to keep an index around, or you can do
as they suggested and just let the current length of the list serve as
your index.


More information about the Python-list mailing list