title: JSON formatting by appending dict values to list
body: <p>I have a JSON object which is like this:</p>
  
  <pre><code>{ "produktNr:"1234",
    "artNr_01":"12",
    "artNr_02":"23",
    "artNr_03":"",
    "artNr_04":"14",
    "name_01":"abc",
    "name_02":"der",
    "test":"junk"
  }
  </code></pre>
  
  <p>I would like to convert this into a dictionary like this:</p>
  
  <pre><code>{ "produktNr:"1234", "artNr":["12","23","","14"], "name":["abc","der"], "test":"junk"}
  </code></pre>
  
  <p>This conversion is based on a sequence given say, <code>seq = ["artNr","name"]</code>. So the contents of the sequence are searched in the dictionary's keys and the values collected into a list. </p>
  
  <p>My attempt so far: </p>
  
  <pre><code>tempDict = {}
  for key,value in fmData.iteritems():
      for seqval in seq:
          if seqval in key:
              if seqval in tempDict:
                  tempDict[seqval].append(value)
              else:
                  x = []
                  x.append(value)
                  tempDict[seqval]=x
          else:
              tempDict[key] = value
  </code></pre>
  
  <p>faces a few problems.</p>
  
  <ul>
  <li><p>The list of values are not ordered i.e, <code>"artNr":["","14","12","23"]</code>
  instead of values of <code>[_01,_02,_03,_04]</code></p></li>
  <li><p>The items cannot be popped from the dictionary since in the loop the dictionary items cannot be deleted resulting in:   </p>
  
  <p>{ "produktNr:"1234", "artNr":["12","23","","14"],"artNr_01":"12", "artNr_02":"23",     "artNr_03":"","artNr_04":"14","name":["abc","der"],"name_01":"abc", "name_02":"der", "test":"junk"}</p></li>
  </ul>
  
  <p>Would love to understand how to deal with this, especially if there's a pythonic way to solve this problem.</p>