[Python-ideas] Question about `list.insert`

Ned Batchelder ned at nedbatchelder.com
Fri Feb 7 12:15:01 CET 2014


On 2/7/14 5:14 AM, Ram Rachum wrote:
> Okay, now that I've posted on python-list, and among the 80% of joke 
> posts in my thread, there was a 20% of posts that assured me that 
> Python's list has no such behavior and that I should use deque, I now 
> ask on Python-ideas: Is there a good reason why `list.insert(whatever, 
> 0)` doesn't opportunistically try to allocate more space at the left 
> side of the list, so as to save the expensive operation of moving all 
> the items? I'm not saying it should reserve space there, just check if 
> that space is available, and if so use it. Is there any reason why not?
>
"check if that space is available": this is not a simple operation. Only 
the memory allocator knows what blocks of memory are allocated and which 
are not.  Memory allocators typically don't support the operation of 
"extend my memory block downward if possible."

Why is deque not the right answer for your problem?

--Ned.


More information about the Python-ideas mailing list