Question about `list.insert`

Dave Angel davea at davea.name
Thu Feb 6 21:54:42 EST 2014


 cool-RR <ram.rachum at gmail.com> Wrote in message:
> Hi,
> 
> I'm curious. If I append an item to a list from the left using `list.insert`, will Python always move the entire list one item to the right (which can be super-slow) or will it check first to see whether it can just allocate more memory to the left of the list and put the item there, saving a lot of resources?
> 

Excellent question.  list does not promise better than O (1)
 behavior,  and CPython in particular will copy, I'm pretty
 sure.

However that's exactly what collections.deque is for.

-- 
DaveA




More information about the Python-list mailing list