Why does creating a list from a list make it larger?

后端 未结 2 735
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 00:49

I\'m seeing some inconsistencies when using sys.getsizeof on what should be identical lists. (Python 2.7.5)

>>> lst = [0,1,2,3,4,5,6,7,         


        
2条回答
  •  忘掉有多难
    2020-12-03 01:15

    With a list literal, the VM creates the list with a set length. When passing a sequence to the list() constructor the elements are added one by one (via list.extend()) and as such the list is resized when appropriate. Since the resize operation overallocates in order to amortize the cost, the final list will usually be larger than the source list.

提交回复
热议问题