title: Creating a List of a List in heavily repeated python functions
body: <p>I'm rather new to python especially when it comes to class attributes and how they work. I've come across this problem where I have a function 'builddata' which outputs a list(Coarsegraining) of a few ints, and sends this to another function 'coarse_grain'.</p>
  
  <p>Over the coarse of the script, these functions are called hundreds of times with CoarseGraining being different every time. What I want to do, is either:</p>
  
  <p>a) Every time CoarseGraining reaches 'coarse_grain' it use that instance, but also saves it to a larger list, which after several repetitions of the function, will contain however many of these different CoarseGraining configurations there are, which can then be used later.</p>
  
  <p>b) Define this process elsewhere, where CoarseGraining is instead sent to 2 functions, where it goes through its usual process in one, but then also is configured into this so called list of a list, which can then be used.</p>
  
  <p>I should also mention, all these functions are defined within the same class 'MultiFitter'. I'd prefer method a) for simplicity reasons, but any possible solutions would be great. Below is a small excerpt of what i'm talking about.</p>
  
  <p>Cheers</p>
  
  <pre><code>class MultiFitter(object):
  
    def __init__(
      self, models, mopt=None, ratio=False, fast=True, extend=False,
          fitname=None, wavg_svdcut=None, **fitterargs
      ):
      super(MultiFitter, self).__init__()
      models = [models] if isinstance(models, MultiFitterModel) else models
      self.models = models
      self.fit = None         # last fit
      self.ratio = ratio
      self.mopt = mopt
      self.fast = fast
      self.extend = extend
      self.wavg_svdcut = wavg_svdcut       
      self.fitterargs = fitterargs        
      self.fitname = (
          fitname if fitname is not None else
          lambda x : x
          )
  
    def builddata(self, data=None, pdata=None, prior=None, mf=None):        
      if mf is None:
          mf = self._get_mf()
          mf['flatmodels'] = self.flatten_models(mf['models'])
      if pdata is None:
          if data is None:
              raise ValueError('no data or pdata')
          pdata = gvar.BufferDict()
          for m in mf['flatmodels']:
  
              M = m.builddata(data)
              CoarseGraining = []
              c1 = 1
              c2 = 0
              for i in range(1, M.shape[0]):                  
                  z = gvar.evalcorr([M[c2],M[i]])
                  corrValue = z[1][0]                
                  if corrValue &gt;= 0.7:
                      c1 = c1 + 1                                              
                      if i == M.shape[0]-1:                            
                          CoarseGraining.append(int(c1))                    
                  else:
                      CoarseGraining.append(int(c1))                        
                      c2 = c2 + c1
                      c1 = 1
                      if i == M.shape[0]-1:                            
                          CoarseGraining.append(int(1))            
              pdata[m.datatag] = (
                  m.builddata(data) if m.ncg &lt;= 1 else                    
                  MultiFitter.coarse_grain(m.builddata(data), CoarseGraining)
                  )
  
    @staticmethod
    def coarse_grain(G, CoarseGraining):
  
      G = numpy.asarray(G)             
      D = []        
      counter = 0
  
      for i, ncg in enumerate(CoarseGraining):            
          D.append(str(numpy.sum(G[..., counter:counter + ncg], axis=-1) / ncg))       
          counter = counter + ncg
  
      D = numpy.asarray(D)
      print(array, 'IS THIS IT???')
      print(D ,'\n')
      #return numpy.transpose([G])
      return G
  </code></pre>