no shift/unshift?

Erik Max Francis max at alcyone.com
Fri Feb 21 18:33:12 EST 2003


"Michael P. Soulier" wrote:

>     I was wondering, is there no built-in way to pull values off and
> on to a
> list in python? append and pop are find if you are working with the
> right
> side, but if you need to work with both, having something like shift
> and
> unshift from perl would be nice.

Use L.insert(0, x) and del L[x].  Remember that since Python lists are
implemented as CS vectors, you suffer from O(n) penalties from
manipulating the left side of the list rather than the right.  If you
expect your sequences to be really long (where these penalties will
really add up), you might want to consider writing your own linked list
class.

>     For consistency, it'd be nice to see append called push, too.
> append and
> pop just sounds wrong when thinking about a stack. :)

So just make your own Stack class that wraps around a list and defines a
push method.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Whom God has put asunder, why should man put together?
\__/ Ralph Waldo Emerson
    Rules for Buh / http://www.alcyone.com/max/projects/cards/buh.html
 The official rules to the betting card game, Buh.




More information about the Python-list mailing list