Predict the output
This tracks how many times a list's storage grows. What does it print?
import sys
xs: list[int] = []
growths = 0
last = sys.getsizeof(xs)
for i in range(10_000):
xs.append(i)
size = sys.getsizeof(xs)
if size != last:
growths += 1
last = size
print(growths < 100, len(xs))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A True 10000
Right: storage grows only a few dozen times for 10,000 appends.
Because each resize makes room for proportionally more items, the copying averages out to a constant per append.
Why not the others?
- B False 10000. Growth is proportional, so resizes are rare.
- C True 100. The list holds all 10,000 items.