import sys
d, size, grew = {}, sys.getsizeof({}), []
for n in range(1, 50):
d[n] = None
if sys.getsizeof(d) != size:
size = sys.getsizeof(d)
grew.append(n)
print(grew)
[1, 6, 11, 22, 43]
The dict's size in memory changed when key number 1, 6, 11, 22 and 43 went in. Key 1 made the first table, with 8 slots. Each later jump is a resize:
The CPython source gives the reason: to keep lookups from slowing down on a near-full table, it resizes the table when it's two thirds full. So 8 slots hold 5 keys, 16 hold 10, 32 hold 21, and 64 hold 42.
When a dict only grows, as here, each resize copies every entry into a table twice as big, which is O(n). But it happens so rarely that inserting n keys costs O(n) in total. Add up the copies above: 5 + 10 + 21 + 42 is 78 for 43 keys, fewer than two per key.
So an insert costs O(1) on average over many inserts. That averaged cost is called amortized O(1).
In the editor, write grow_points(n): the key numbers, from 1 to n, that make the table grow. Key 1 makes a table of 8 slots, a table of s slots holds at most 2 * s // 3 keys, and each growth doubles it. Then write copies(n): how many entries the resizes copy while n keys go in.