Tying to reduce memory space taken by a large dictionary I changed dictionary's structure from initially {string: boolean}
to {int: boolean}
. To my surprise, the memory used by the dictionary remained the same:
print(sys.getsizeof(myDictionary))
>140584
Could you explain to my why using int (of size 24 bytes) instead of strings (of at least 60 bytes, probably more due to my data type) doesn't help reducing the full dictionary size? Is it because both already link to an object?
Here is how the dictionary is computed:
- for
{string: boolean}
dictionary
myDictionary ={feat:(feat in item_feature_list) for feat in model_features_list}
- for
{int: boolean}
dictionary
myDictionary = {int(i):(feat in item_feature_list) for feat, i in enumerate (model_features_list)}
thanks.