no shift/unshift?

Larry Bates lbates at swamisoft.com
Mon Mar 24 18:24:39 EST 2003


pop(0) pops from the left side of a list.

actually pop(n) pops from "anywhere" in the list
with default pop() being right side (last element)
of the list.

Larry

"Gerrit Holl" <gerrit at nl.linux.org> wrote in message
news:mailman.1045861787.29224.python-list at python.org...
> Michael P. Soulier schreef op vrijdag 21 februari om 21:24:23 +0000:
> >     Hello,
> >
> >     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.
> >     If I just need to simulate a stack, I'll reverse the list if need
be, but
> > sometimes I want to manipulate both sides.
>
> Does this do what you want?
>
>  19 >>> class Mylist(list):
>  19 ...  def shift(self, val):
>  19 ...   self.insert(0, val)
>  19 ...   return self.pop()
>  19 ...
>  20 >>> l=Mylist(range(5))
>  21 >>> l
> [0, 1, 2, 3, 4]
>  22 >>> l.shift('bla')
> 4
>  23 >>> l
> ['bla', 0, 1, 2, 3]
>
> yours,
> Gerrit.
>
> --
> Asperger Syndroom - een persoonlijke benadering:
> http://people.nl.linux.org/~gerrit/
> Het zijn tijden om je zelf met politiek te bemoeien:
> http://www.sp.nl/
>






More information about the Python-list mailing list