title: How to execute a for loop in batches?
body: <pre><code>for x in records:
     data = {}
     for y in sObjectName.describe()['fields']
           data[y['name']] = x[y['name']]
     ls.append(adapter.insert_posts(collection, data))
  </code></pre>
  
  <p>I want to execute the code ls.append(adapter.insert_post(collection, x)) in the batch size of 500, where x should contain 500 data dicts. I could create a list a of 500 data dicts using a double for loop and a list and then insert it. I could do that in the following way, , is there a better way to do it? :</p>
  
  <pre><code>for x in records:
      for i in xrange(0,len(records)/500):
          for j in xrange(0,500):
              l=[]
              data = {}
              for y in sObjectName.describe()['fields']:
                  data[y['name']] = x[y['name']]
                  #print data
              #print data
              l.append(data)
          ls.append(adapter.insert_posts(collection, data))
  
      for i in xrange(0,len(records)%500):
          l=[]
          data = {}
          for y in sObjectName.describe()['fields']:
              data[y['name']] = x[y['name']]
              #print data
          #print data
          l.append(data)
      ls.append(adapter.insert_posts(collection, data))
  </code></pre>