Popping from the middle of a deque + deque rotation speed

Tim Chase python.list at tim.thechases.com
Fri Apr 28 19:13:01 EDT 2006


> Does anyone have an easier/faster/better way of popping from the middle
> of a deque than this?
> 
> class mydeque(deque):
>   def popmiddle(self, pos):
>     self.rotate(-pos)
>     ret = self.popleft()
>     self.rotate(pos)
>     return ret

My first thought would just be to use indexing:

	def popmiddle(self, pos):
		ret = self[pos]
		del(self[pos])
		return ret

It seems to work with my Python2.4 here.  If you're 
interested in efficiency, I'll leave their comparison as an 
exercise to the reader... :)

-tkc






More information about the Python-list mailing list