title: How do I make Python make all identical strings use the same memory?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1136826/what-does-python-intern-do-and-when-should-it-be-used">What does python <em>intern</em> do, and when should it be used?</a>  </p>
  </blockquote>
  
  
  
  <p>I am working with a program in python that has to correlate on an array with millions of string objects. I've discovered that if they all come from the same quoted string, each additional "string" is just a reference to the first, master string. However if the strings are read from a file, and if the strings are all equal, each one still requires new memory allocation.</p>
  
  <p>That is, this takes about 14meg of storage:</p>
  
  <pre><code>a = ["foo" for a in range(0,1000000)]
  </code></pre>
  
  <p>While this requires more than 65meg of storage:</p>
  
  <pre><code>a = ["foo".replace("o","1") for a in range(0,1000000)]
  </code></pre>
  
  <p>Now I can make the memory take much less space with this:</p>
  
  <pre><code>s = {"f11":"f11"}
  a = [s["foo".replace("o","1")] for a in range(0,1000000)]
  </code></pre>
  
  <p>But that seems silly. Is there an easier way to do this?</p>