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(

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.

Published on 18-Apr-2018 17:06:15