title: Adding elements to a dictionary via for loop
body: <p>Python 3.6</p>
  
  <p>All debug output is from PyCharm 2017.1.2</p>
  
  <p>I have a program that is working with one flaw.</p>
  
  <p>Here is the piece I am having a problem with:</p>
  
  <pre><code>def striplist(errlist):
      return [item.strip() for item in errlist]
  
  for line in shintlist:
      errors_list = striplist(line.split(","))
  
      errRegex = re.compile(r'(\d+)(\s)(\D+)')
  
      errdict = {}
      for elem in errors_list:
          mo = errRegex.search(elem)
          errdict[mo.group(3)] = int(mo.group(1))
          if int(mo.group(1)) &gt; 0:
              print("There are errors")
          else:
              print("There are no errors")
  
      print(errdict)
  </code></pre>
  
  <p>** NOTE** The print statements are just for me to use at this time
  to check the program logic.  They will be removed later.</p>
  
  <p>At the time these instructions execute, 'shintlist'
  is a list of comma separated strings.</p>
  
  <pre><code>__len__ = {int} 9
  0 = {str} '     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored'
  1 = {str} '     0 watchdog'
  2 = {str} '     0 input packets with dribble condition detected'
  3 = {str} '     5912 packets output, 738736 bytes, 0 underruns'
  4 = {str} '     0 output errors, 0 collisions, 2 interface resets'
  5 = {str} '     4 unknown protocol drops'
  6 = {str} '     0 babbles, 0 late collision, 0 deferred'
  7 = {str} '     0 lost carrier, 0 no carrier'
  8 = {str} '     0 output buffer failures, 0 output buffers swapped out'
  </code></pre>
  
  <p>The above code works.  It correctly splits on the ',' and then strips
  off the white space.  It then creates a dictionary key:value
  entry in errdict{}:</p>
  
  <pre><code>errdict = {dict} {'input errors': 0}
   __len__ = {int} 1
   'input errors' (64451912) = {int} 0
  </code></pre>
  
  <p>Where I am stuck is this:  With each iteration errdict{} is overwritten
  with the next line from 'shintlist'.</p>
  
  <p>What I want to do is:</p>
  
  <p>If every single value is zero (0) for every single individual item:
      if int(mo.group(1)) > 0:
  then skip this interface.  If ANY one value is greater than zero then
  I want the dictionary 'errdict{}' to contain ALL of the above lines
  from 'shintlist' so I have a single dictionary with everything.</p>
  
  <p>** NOTE 2** These entries: 5912 packets output, 738736 bytes, will always
  be non-zero for an active interface.  I will deal with these later.</p>
  
  <p>Each of these lines is the state of errdict{} when each line in 'shintlist'
  is processed:</p>
  
  <pre><code>{'input errors': 0, 'CRC': 0, 'frame': 0, 'overrun': 0, 'ignored': 0}
  {'watchdog': 0}
  {'input packets with dribble condition detected': 0}
  {'packets output': 6304, 'bytes': 786190, 'underruns': 0}
  {'output errors': 0, 'collisions': 0, 'interface resets': 2}
  {'unknown protocol drops': 4}
  {'babbles': 0, 'late collision': 0, 'deferred': 0}
  {'lost carrier': 0, 'no carrier': 0}
  {'output buffer failures': 0, 'output buffers swapped out': 0}
  </code></pre>
  
  <p>I want a single dictionary with all lines and values if any single value is
  non-zero so I can print off a report showing all interfaces with errors.</p>
  
  <p>Unless I am way off, I am asking how do append each line to
  errdict{} instead of overwriting?</p>
  
  <p>Thank you.</p>