title: Different ways of Initializing list in Python
body: <p>Might be a naive question but this troubled me a lot. I want to initialize a list with some default values. I consider the following 2 approaches to do so:</p>
  
  <p>1.<code>cache1 = [[-1 for _ in range(5)] for _ in range(5)]</code><br>
  2. <code>cache2 = [[-1]*5]*]</code>  </p>
  
  <p>I was under the assumption that their behavior is exactly the same. However, when I execute the following commands:</p>
  
  <pre><code>cache1[0][0] = 100
  cache2[0][0] = 100
  </code></pre>
  
  <p>The lists are updated to:  </p>
  
  <pre><code>cache1 = [[100, -1, -1, -1, -1], [100, -1, -1, -1, -1], [100, -1, -1, -1, -1], [100, -1, -1, -1, -1], [100, -1, -1, -1, -1]]
  cache2 = [[100, -1, -1, -1, -1], [100, -1, -1, -1, -1], [100, -1, -1, -1, -1], [100, -1, -1, -1, -1], [100, -1, -1, -1, -1]]
  </code></pre>
  
  <p>Can someone please point out what is the difference between the two initializations? Thank you</p>