title: Get reference to Python dict key
body: <p>In Python (3.7 and above) I would like to obtain a reference to a dict <strong>key</strong>. More precisely, let <code>d</code> be a dict where the keys are strings. In the following code, the value of <code>k</code> is potentially stored at <strong>two</strong> distinct locations in memory (one pointed to by the dict and one pointed to by <code>k</code>), whereas the value of <code>v</code> is stored at only <strong>one</strong> location (the one pointed to by the dict).</p>
  <pre class="lang-py prettyprint-override"><code># 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
  </code></pre>
  <p>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 <code>k</code> with a pointer to the corresponding string used by <code>d</code> before storing <code>k</code> in other data structures. Is there a straightforward way of doing this, that is using the keys of the dict as a string pool?</p>
  <p>(<strong>Footnote:</strong> this may seem as premature optimisation, and perhaps it is, but being an old-school C programmer I sleep better at night doing &quot;memory tricks&quot;. 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.)</p>