How to optimize Python dictionary memory usage?


There are some cases where you can simply avoid using dictionaries in python. For example, if you're creating a dict of continuous integers to some values, consider using a list instead.

If you're creating string-based keys, you might be better off using a Trie data structure(http://en.m.wikipedia.org/wiki/Trie).

There are other cases where you can replace the use of dicts by some other less memory intensive data structure.

But you need to understand that at some places, you have to use a dict as it helps in optimization. The python dict is a relatively straightforward implementation of a hash table. This is how hash tables are implemented in most languages like Java, C++, etc.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

479 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements