title: Split dictionary by keys in Python
body: <p>I would like to clarify this code, especially variables. I am a newbie in <code>python</code>.  </p>
  
  <p><strong>GOAL:</strong><br>
  I would like split data dictionary <code>pairs</code> by <code>keys</code> of this dictionary. The output is list of lists of class <code>Ward</code>. I think, my solution is too complicated, is another better solution?</p>
  
  <pre><code>class Ward:
      def __init__(self, code, data):
          self.code = code
          self.data = data
  
  
  def prepare_data_for_templates(cs, h, f):       
      pairs = {'201': ['&lt;tr&gt;&lt;td&gt;Dunajsk&amp;#225; Streda&lt;/td&gt;&lt;td&gt;201&lt;/td&gt;&lt;/tr&gt;\n', '&lt;tr&gt;&lt;td&gt;Dunajsk&amp;#253; Kl&amp;#225;tov&lt;/td&gt;&lt;td&gt;201&lt;/td&gt;&lt;/tr&gt;\n'], '205': ['&lt;tr&gt;&lt;td&gt;Ko&amp;#353;oln&amp;#225;&lt;/td&gt;&lt;td&gt;205&lt;/td&gt;&lt;/tr&gt;\n',]}
  
      print "Pairs: " + str(sorted(pairs.keys())) + "\n"
  
      #output data - ba, tt...
      OUT = []
  
      BA = []
      TT = []
  
      for k, v in sorted(pairs.iteritems()):
          #print k + "\n", v
          if int(k) &lt; 199:
              BA.append( Ward(k, v )
          elif int(k) &lt; 299:
              TT.append( Ward(k, v )
  
      OUT.append(BA)
      OUT.append(TT)
  
  
      for j in OUT:
          for i in j:
              print i.code
  
      return OUT
  </code></pre>
  
  <p><strong>EDIT</strong>: Thanks for the answer, I updated my code using JSON.</p>
  
  <p>tab01.json:</p>
  
  <pre><code>{
      "data": [
          {
              "id": "101", "c01": "mun1"
          },
          {
              "id": "101", "c01": "mun2"
          },
          {
              "id": "205", "c01": "mun3"
          },
          {
              "id": "205", "c01": "mun4"
          },
          {
              "id": "205", "c01": "mun5"
          }
      ]
  }
  </code></pre>
  
  <p>code.py:</p>
  
  <pre><code>import os, json
  
  def prepare_data_for_templates(file):       
  
      pairs = {}
      codes = []
  
      with open(file, "r") as input:
          json_obj = json.load(input)
  
      for d in json_obj["data"]:
          codes.append((str(d["id"]), d))
  
      for c in codes:
          pairs.setdefault(str(c[0]), []).append(c[1])
  
      for k, v in pairs.iteritems():
          with open( str(k) + '.json', 'w') as outfile:
              json.dump(v, outfile)
  
  prepare_data_for_templates("tab01.json")
  </code></pre>