I've read that python lists are supposed to take 64 bytes, and that every entry in a list is supposed to 'cost' 8 additional bytes. I decided to test this out. However, while testing this, I discovered that this depends on how you add items to your list. Why are sys.getsizeof
for ob1
and obj2
not consistent in the code below?
import sys
test1 = 'This is a string.'
obj1 = []
obj2 = ['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', '.']
obj3 = list(test1)
for i in range(len(test1)):
obj1.append(test1[i])
print(sys.getsizeof(obj1))
print(obj1)
print(sys.getsizeof(obj2))
print(obj2)
print(sys.getsizeof(obj3))
print(obj3)
>>>264
>>>['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', '.']
>>>200
>>>['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', '.']
>>>264
>>>['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', '.']
obj2
reports the size that I expected (64+8*17=200). What is the overhead after using the append
-function, and can that overhead somehow be removed after constructing the list?
I've read through this related topic here, but I don't think they have the same answer as the other one seemed to be related to Pandas.