About the implementation of del in Python 3

Terry Reedy tjreedy at udel.edu
Thu Jul 6 03:29:42 EDT 2017


On 7/6/2017 3:08 AM, Dan Wissme wrote:
> I thought that del L[i] would slide L[i+1:] one place to the left, 
> filling the hole, but :
>> >>> L
> [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
> >>> id(L)
> 4321967496
> >>> id(L[5]) # address of 50 ?
> 4297625504
> >>> del L[2]
> >>> id(L[4]) # new address of 50 ?
> 4297625504
> >>> id(L)
> 4321967496

> So the element 50 is still at the same memory location.
> What del L[i] do exactly, and what is its complexity ? O(1) or O(n) ?

A list is an array of references to objects that exist outside of the 
list. Del deleted a reference, not any of the objects.
-- 
Terry Jan Reedy


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /