[Python-Dev] list.shift()

Moshe Zadka Moshe Zadka <mzadka@geocities.com>
Sat, 18 Mar 2000 07:27:13 +0200 (IST)


On Fri, 17 Mar 2000, Ka-Ping Yee wrote:

> 
> Has list.shift() been proposed?
> 
>     # pretend lists are implemented in Python and 'self' is a list
>     def shift(self):
>         item = self[0]
>         del self[:1]
>         return item
> 
> This would make queues read nicely... use "append" and "pop" for
> a stack, "append" and "shift" for a queue.

Actually, I once thought about writing a Deque in Python for a couple
of hours (I later wrote it, and then threw it away because I had nothing
to do with it, but that isn't my point). So I did write "shift" (though
I'm certain I didn't call it that). It's not as easy to write a
maintainable yet efficient "shift": I got stuck with a pointer to the 
beginning of the "real list" which I incremented on a "shift", and a
complex heuristic for when lists de- and re-allocate.

I think the tradeoffs are shaky enough that it is better to write it in 
pure Python rather then having more functions in C (whether in an old
builtin type rather then a new one). Anyone needing to treat a list as a 
Deque would just construct one

l = Deque(l)

built-in-functions:-just-say-no-ly y'rs, Z.
--
Moshe Zadka <mzadka@geocities.com>. 
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com