python get list memory size 1

python get list memory size

>>> import sys
>>> sys.getsizeof([])
36
>>> sys.getsizeof([1])
40
>>> lst = []
>>> lst.append(1)
>>> sys.getsizeof(lst)
52
>>> 

Here is what the above code is Doing:
1. We import the sys module.
2. We use the getsizeof() function to get the size of an empty list.
3. We use the getsizeof() function to get the size of a list with one element.
4. We create an empty list and assign it to the variable lst.
5. We append an element to the list.
6. We use the getsizeof() function to get the size of the list.

The output shows that the size of an empty list is 36 bytes.
The size of a list with one element is 40 bytes.
The size of a list with one element is 52 bytes.

The size of a list increases by 16 bytes when we add an element to it.

Similar Posts