title: Correct way to fill a list from a SQL query using pyodbc
body: <p>Is this the correct way to get a list from a SQL query in Python 2.7? Using a loop just seems somehow spurious. Is there a neater better way?</p>
  
  <pre><code>import numpy as np
  import pyodbc as SQL
  from datetime import datetime
  
  con = SQL.connect('Driver={SQL Server};Server=MyServer; Database=MyDB; UID=MyUser; PWD=MyPassword')
  cursor = con.cursor()
  
  #Function to convert the unicode dates returned by SQL Server into Python datetime objects
  ConvertToDate = lambda s:datetime.strptime(s,"%Y-%m-%d")
  
  #Parameters
  Code = 'GBPZAR'
  
  date_query = '''
               SELECT DISTINCT TradeDate 
               FROM MTM 
               WHERE Code = ? 
                 and TradeDate &gt; '2009-04-08' 
               ORDER BY TradeDate
              '''
  
  #Get a list of dates from SQL
  cursor.execute(date_query, [Code])
  rows = cursor.fetchall()
  Dates = [None]*len(rows) #Initialize array
  r = 0
  for row in rows:
      Dates[r] = ConvertToDate(row[0])
      r += 1
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>What about when I want to put a query into a structured array? At the moment I do something like this:</p>
  
  <pre><code>#Initialize the structured array
  AllData = np.zeros(num_rows, dtype=[('TradeDate', datetime),
                                      ('Expiry', datetime), 
                                      ('Moneyness', float),
                                      ('Volatility', float)])
  
  #Iterate through the record set using the cursor and populate the structure array
  r = 0
  for row in cursor.execute(single_date_and_expiry_query, [TradeDate, Code, Expiry]):
      AllData[r] = (ConvertToDate(row[0]), ConvertToDate(row[1])) + row[2:] #Convert th0e date columns and concatenate the numeric columns
      r += 1
  </code></pre>