title: build function to loop over dictionary of dictionaries to compute something from every nested dictionary individually
body: <p>very new to python and programming in general so bear with me. So the basic function loops over a simple dictionary, checks the values and if one of them is 0 it replaces that value with the mean of the rest of the group. This works!</p>
  
  <pre><code>def replace_zero(group):
  for k, v in group.iteritems():
      if v == '-':
          print 'there was a - value!'
          group[k] = 0
          new_mean = sum(group.itervalues()) / (len(group.keys())-1)
          group[k] = new_mean
          return group[k]
      elif v == 0:
          print 'there was a zero value!'
          group[k] = 0
          new_mean = sum(group.itervalues()) / (len(group.keys())-1)
          group[k] = new_mean
          return group[k]
  </code></pre>
  
  <p>But due to my huge dataset i don't want to call this function 36 times, so i made 12 dictionaries, which contain 3 dictionaries each. </p>
  
  <pre><code>gr_ctr_0 = {'distance': {'A1': sheet['E5'].value, 'A12': sheet['E16'].value,
                       'B1': sheet['E17'].value, 'B12': sheet['E28'].value,
                       'C1': sheet['E29'].value, 'C12': sheet['E40'].value,
                       'D1': sheet['E41'].value, 'D12': sheet['E52'].value},
          'speed': {'A1': sheet['F5'].value, 'A12': sheet['F16'].value,
                    'B1': sheet['F17'].value, 'B12': sheet['F28'].value,
                    'C1': sheet['F29'].value, 'C12': sheet['F40'].value,
                    'D1': sheet['F41'].value, 'D12': sheet['F52'].value},
          'time': {'A1': sheet['G5'].value, 'A12': sheet['G16'].value,
                   'B1': sheet['G17'].value, 'B12': sheet['G28'].value,
                   'C1': sheet['G29'].value, 'C12': sheet['G40'].value,
                   'D1': sheet['G41'].value, 'D12': sheet['G52'].value}}
  </code></pre>
  
  <p>I would now like to extend my function so that i pass it one dictionary (gr_ctr_0), and it still replaces any 0 value with the mean of the nested dictionary (e.g inside 'distance')</p>
  
  <p>I've read all the related questions and thought it would a relatively simple line where I just add something along the lines of </p>
  
  <pre><code>def replace_zero(main_dict):
      for group in main_dict:
          for k, v in group.iteritems():
  </code></pre>
  
  <p>but it doesn't work, like at all :( Alternatively I read something about the recursive function, however I would have no idea how to implement that! Thank you all in advance!</p>
  
  <p>EDIT !!!</p>
  
  <p>taking both comments into account, I came up with this solution</p>
  
  <pre><code>def replace_zero_stackoverflow(group):
      average = float(sum(group.itervalues())) / (len(group)- sum(v==0 for v in group.itervalues()))
      for k, v in group.iteritems():
          if v == 0:
              group[k] = average
      return group
  res = {name: replace_zero_stackoverflow(group) for name, group in gr_ctr_0.iteritems()}
  </code></pre>