title: Why does this assigned object share the same memory space as the original object?
body: <p>In python I came across this strange phenomena while working with itertools groupby module.</p>
  
  <p>In python, variable assignment means assigning the new variable its own memory instead of a pointer to the original memory (from my understanding if this is incorrect please let me know):</p>
  
  <pre><code>y = 7
  x = y    
  y = 9
  
  x will still be 7
  </code></pre>
  
  <p>Yet when I was working with groupby module, I was using this module to group items that had the same key into one group. I wanted two groups since reiterating over the original group was useless as the memory would have already been modified. Example:</p>
  
  <pre><code>for key, group in groupby(rows, lambda x: x[0]):
  
      data = [thing[1] for thing in group] #accesses 1st attribute of element
      data2 = [thing[2] for thing in group] # would yield [] as group is empty
  </code></pre>
  
  <p>So I tried this instead:</p>
  
  <pre><code>for key, group in groupby(rows, lambda x: x[0]):
      #create a copy of group to reiterate over
      toup = group
  
      print toup #&lt;itertools._grouper object at 0x1039a8850&gt;
      print group #&lt;itertools._grouper object at 0x1039a8850&gt;
  
      data = [thing[1] for thing in group] #accesses 1st attribute of element
      data2 = [thing[2] for thing in toup]
  </code></pre>
  
  <p>data2 should access the 2nd item but yields [] since they both share the same memory</p>
  
  <p>My question is why does this happen? Shouldn't assigning group to toup means toup would have a copy of groups memory at a different hex address location?</p>
  
  <p>Also what can I do to circumvent this problem so I don't have to write two groupby iterations?</p>