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

PythonListPython Internals

Python Problem Overview


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,8,9]
>>> sys.getsizeof(lst)
76
>>> lst2 = list(lst)
>>> sys.getsizeof(lst2)
104
>>> lst3 = list(lst2)
>>> sys.getsizeof(lst3)
104
>>> sys.getsizeof(lst[:])
76
>>> sys.getsizeof(lst2[:])
76

Does anybody have a simple explanation?

Python Solutions


Solution 1 - Python

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 https://hg.python.org/releasing/3.4.2/file/10298f4f42dc/Objects/listobject.c#l781">`list.extend()`</a>;) and as such the list is resized when appropriate. Since the https://hg.python.org/releasing/3.4.2/file/10298f4f42dc/Objects/listobject.c#l25">resize operation overallocates in order to amortize the cost, the final list will usually be larger than the source list.

Solution 2 - Python

When you create a list literal, the size reported is the minimum size needed to hold the data. You can see this because the size jumps up if you append a single element. However, when you use list to copy it, it allocates some extra space - it takes a few appends before it reallocates (in your case, I suspect the 8th append will do it - it needs 4 more bytes per element). There is probably a reason why these allocation behaviors are different, but I'm not sure what that might be.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark RansomView Question on Stackoverflow
Solution 1 - PythonIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 2 - PythonAaron DufourView Answer on Stackoverflow