In Python (3.7 and above) I would like to obtain a reference to a dict key. More precisely, let d
be a dict where the keys are strings. In the following code, the value of k
is potentially stored at two distinct locations in memory (one pointed to by the dict and one pointed to by k
), whereas the value of v
is stored at only one location (the one pointed to by the dict).
# d is a dict
# k is a string dynamically constructed, in particular not from iterating over d's keys
if k in d:
v = d[k]
# Now store k and v in other data structures
In my case, the dict is very large and the string keys are very long. To keep memory usage down I would like to replace k
with a pointer to the corresponding string used by d
before storing k
in other data structures. Is there a straightforward way of doing this, that is using the keys of the dict as a string pool?
(Footnote: this may seem as premature optimisation, and perhaps it is, but being an old-school C programmer I sleep better at night doing "memory tricks". Joke aside, I do genuinely would like to know the answer out of curiosity, and I am indeed going to run my code on a Raspberry Pi and will probably face memory issues.)