Chaitanya Baweja
Oct 12, 2019 · 1 min read

I do feel that getsizeof() is not extremely straightforward as it should be.

But, you are wrong on one aspect. It does give different values for different dictionaries.

The memory taken by a dictionary is dependent upon the number of (key,value) pairs it needs to store. Python initially keeps enough memory for 5 (key, value) pairs. But, when you need the 6th pair, more memory is acquired.

Try executing the following:

import sys, itertools
>>> sys.getsizeof(dict(zip(range(1), itertools.cycle([1]))))
>>> sys.getsizeof(dict(zip(range(5), itertools.cycle([1]))))
>>> sys.getsizeof(dict(zip(range(6), itertools.cycle([1]))))

You can find more info on this : here

    Chaitanya Baweja

    Written by

    Machine Learning Engineer | Python | Data Analytics | Economics | Physics

    More From Medium

    More from Chaitanya Baweja

    More from Chaitanya Baweja

    More from Chaitanya Baweja

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade