no shift/unshift?
Gerrit Holl
gerrit at nl.linux.org
Fri Feb 21 16:07:03 EST 2003
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