title: How to sort uneven dictionary by key and create CSV
body: <p>I have a python dictionary which for each KEY one can have a variable number of VALUES (arranged in a list).</p>
  
  <p>For example:</p>
  
  <pre><code>{'607': [36146], '448': [50890, 44513], '626': [44349, 44436]}
  </code></pre>
  
  <p>What I'd like to do is generate a CSV of this information with a format like so:</p>
  
  <pre><code> 448 , 607 , 626
  50890,36146,44349
  44513,     ,44436
  </code></pre>
  
  <p>Currently my code can produce a CSV such as this, the only issue being that the columns of the CSV are not sorted according to the ascending numerical order of the KEYs. My code so far is below:</p>
  
  <pre><code>csv_file = 'file.csv'
  with open(csv_file, 'wb') as fd:
      writer = csv.writer(fd, delimiter = ',')
  
      # Format headers for aesthetics
      csv_headers = [' {} '.format(elem) for elem in dictionary.keys()]
  
      writer.writerow(headers)
  
      # Format data to create convenient csv format
      csv_data = itertools.izip_longest(*dictionary.values(), fillvalue = '     ')
      writer.writerows(csv_data)
  </code></pre>
  
  <p>As you can see I split the KEYs from the VALUEs and write them separately but if I want to sort the columns by the KEYs I imagine this is probably not the best way to go about this. Therefore, I was hoping someone could point me in the right (and most pythonic) direction.</p>