Dynamically growing an array to implement a stack

Lie Ryan lie.1296 at gmail.com
Fri Apr 9 02:31:39 CEST 2010


On 04/09/10 06:54, M. Hamed wrote:
> Thanks Patrick, that is what I was exactly looking for.
> 
> Paul, thanks for your example. wasn't familiar with the stack class. 

The stack class is nothing but a wrapper that renames append() to
push(); everything you need can be fulfilled by the regular list.

> I feel Patrick's method is a lot simpler for my purpose.

No you don't.

>> Well, if you never want to add intermediate data between your new
>> element and the stack, you can just do:
>>
>> stack[index:index + 1] = [newelement]

that is effectively the same as:

stack.insert(index, newelement)

But if you really want to use list as a stack, you don't want to manage
your stack pointer manually; let `list` manage the stack pointer for you
and use .append() and .pop()



More information about the Python-list mailing list