title: References to mutables (e.g., lists) as values in Python dictionaries - what is best practice?
body: <p>It is possible to map a dictionary key to a value that is a reference to a
  mutable object, such as a list.  Such a list object can be changed by invoking
  a list method on the reference, and the changes will be reflected in the
  dictionary.  This is discussed in:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference">Python: How do I pass a variable by reference?</a> and </li>
  <li><a href="https://stackoverflow.com/questions/9696495/python-when-is-a-variable-passed-by-reference-and-when-by-value">Python : When is a variable passed by reference and when by value?</a></li>
  </ul>
  
  <p><strong>My question</strong></p>
  
  <p>Is it a <em>good idea</em> to map a dictionary key to the <em>reference</em> of a mutable
  object as opposed to mapping the key to an unnamed value?</p>
  
  <p>In other words, it
  is better to create a dictionary with:</p>
  
  <pre><code>In [74]: x = {'a': somelist}
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>In [74]: x = {'a': somelist[:]}
  </code></pre>
  
  <p>And is it better to change a dictionary value though citing that value
  by reference, e.g.:</p>
  
  <pre><code>In [77]: somelist.remove('apples')
  </code></pre>
  
  <p>or by its dictionary index:</p>
  
  <pre><code>In [77]: x['a'].remove('apples')
  </code></pre>
  
  <p><strong>Discussion and research</strong></p>
  
  <p>A reference provides a nice handle that can improve the readability of, say, a
  function.  As far as I can tell, however, the fact that a dictionary value was
  originally bound to a reference is lost once the value is created; one cannot
  see this fact when displaying a dictionary.  </p>
  
  <p>On the other hand, I am not sure this matters, because the reference and the
  value are the same object, and if one deletes the reference, the value object
  itself remains.</p>
  
  <p>As I see it:</p>
  
  <pre><code>In [73]: somelist = ['apples', 'oranges', 'lemons', 'tangerines']
  In [74]: x = {'a': somelist}
  In [75]: x
  Out[75]: {'a': ['apples', 'oranges', 'lemons', 'tangerines']}
  </code></pre>
  
  <p>In dictionary <code>x</code>, key <code>'a'</code> maps to value <code>somelist</code>, though I do not 
  see a way to verify that the value is associated with the reference
  <code>somelist</code>.</p>
  
  <pre><code>In [76]: x['a'] is somelist
  Out[76]: True
  </code></pre>
  
  <p>This confirms that the list I see as the value is the same as the object
  pointed to by <code>somelist</code>.</p>
  
  <pre><code>In [77]: x['a'].remove('apples')
  In [78]: x
  Out[78]: {'a': ['oranges', 'lemons', 'tangerines']}
  </code></pre>
  
  <p>Since the value of <code>'a'</code> in dictionary <code>x</code> is a list, I can remove an item
  from the list using the list method <code>remove</code> on object <code>x['a']</code>.</p>
  
  <pre><code>In [79]: somelist.remove('lemons')
  In [80]: x
  Out[80]: {'a': ['oranges', 'tangerines']}
  </code></pre>
  
  <p>Alternatively, I can use the method <code>remove</code> on object <code>somelist</code>.</p>
  
  <pre><code>In [81]: del somelist
  </code></pre>
  
  <p>I can delete the reference to the list, but the list object itself remains
  as the value associated with key <code>a</code>.</p>
  
  <pre><code>In [82]: x['a'].remove('oranges')
  
  In [83]: x
  Out[83]: {'a': ['tangerines']}
  </code></pre>