title: Python Numerical Error Arising in Series of Nested Lists
body: <p>I'm attempting to write a script to parse a very large data file with information on location and time, and store that information in an array (i.e. a list of lists), however, for some reason my code is writing the same numbers across multiple sub-lists.</p>
  
  <pre><code># Import data from inputFile into list
  with open(r"C:\..file.dat") as inputFile:
      inputList = list(inputFile)
  
  totalSegs = 775
  totalPrds = 938
  
  stressPrd = 1
  segNum = 1
  testList = []
  masterList = []
  
  while stressPrd &lt;= totalPrds: #build a list of times
      testList.append(0)
      stressPrd += 1
  
  while segNum &lt;= totalSegs: #build a list of locations
      masterList.append(testList) #list of time periods by location
      segNum += 1
  
  stressPrd = 1
  segNum = 1
  
  for inputItem in inputList: #read data from file and write to lists
      if inputItem != '\n'
          inputItem = inputItem.split()
          if int(inputItem[3]) == int(segNum):
              testVar = float(masterList[segNum - 1][stressPrd - 1])
              testVar += float(inputItem[6])
              masterList[segNum - 1][stressPrd - 1] = testVar
          else:
              segNum += 1
              if segNum &lt;= totalSegs:
                  testVar = float(masterList[segNum - 1][stressPrd - 1])
                  testVar += float(inputItem[6])
                  masterList[segNum - 1][stressPrd - 1] = testVar
              else:
                  segNum = 1
                  stressPrd += 1
                  testVar = float(masterList[segNum - 1][stressPrd - 1])
                  testVar += float(inputItem[6])
                  masterList[segNum - 1][stressPrd - 1] = testVar
  </code></pre>
  
  <p>What should happen is that the data for the same location (segNum) from the same time (stressPrd) is summed and stored in a sublist organized by stressPrd, and then that list for a given location across time is stored by segNum in the parent list. What is happening, however, is that the script instead sums all data across all locations for a given time (stressPrd), and that sum is stored in every location sublist.</p>
  
  <p>I've tried inserting some print statements to track what's happening to multiple sub-lists simultaneously, and they all start tallying the same sum at the same time. My variables segNum and stressPrd both seem to increment correctly, and as near as I can tell my If statements are executing correctly, so I'm having trouble figuring out the source of the problem.</p>
  
  <p>As a very simplified example of the result I want:</p>
  
  <pre><code>Data:
  Loc    Time1    Time2
  A      6        1
  A      2        2
  B      2        3
  C      5        4
  C      1        1
  
  Result:
  [[8,3],[2,3],[6,5]]
  </code></pre>
  
  <p>Thanks in advance!</p>