title: Convert array of dictionaries into 2 dimensional array with potentially non-matching fieldnames
body: <p>I'm using <code>csv.Dictreader</code> to read in a csv file because accessing items by fieldnames is very convenient, but the the <code>csv.Dictwriter</code> class is very finicky about how it deals with fieldnames. I get lots of <code>"dict contains fields not in fieldnames"</code> exceptions, when I specifically don't want the dict to contain all the fieldnames I provided. I also want to be able to provide a list of fieldnames with keys that might not appear in every row of dictionary list.</p>
  
  <p>So I've created a way to convert a list of dictionaries to a 2d array I can use with the <code>csv.writer.writerow</code> function. </p>
  
  <p><strong>Question:</strong></p>
  
  <p>What I'm wondering is if my method is good, bad, or ugly. Is there a better/more pythonic way of converting a list of dictionaries with arbitrary fieldnames to a 2d array? Am I missing something obvious with <code>csv.DictWriter</code>?</p>
  
  <p><strong>Code:</strong></p>
  
  <p>What it does is:</p>
  
  <p>The output will skip fieldnames that you don't provide, but will also just put a blank space if you provide a fieldname that doesn't appear in every (or any) row, but will still include it in the header at the top of the csv file.</p>
  
  <pre><code>def csvdict_to_array(dictlist, fieldnames):
      # Start with header row
      csv_array = [fieldnames]
  
      for row in dictlist:
          csv_array.append(dictlist_row_to_list(row, fieldnames))
  
      return csv_array
  
  def dictlist_row_to_list(dictlist_row, fieldnames):
      csv_row = []
  
      for field in fieldnames:
          if field not in dictlist_row:
              csv_row.append('')
          else:
              csv_row.append(dictlist_row[field])
  
      return csv_row
  </code></pre>
  
  <p>Sample input/output:</p>
  
  <pre><code>fieldnames = ["one", "three", "ten"]
  dictlist = [{"one": "bob", "two": "bill", "three":"cat"},
              {"one": "john", "two": "jack", "ten":"dog"}]
  
  Output:
  one,three,ten
  bob,cat,
  john,,dog
  </code></pre>
  
  <p>Thanks for your time</p>