title: Creating sublists
body: <p>The opposite of list flattening.</p>
  
  <p>Given a list and a length n return a list of sub lists of length n.</p>
  
  <pre><code>def sublist(lst, n):
      sub=[] ; result=[]
      for i in lst:
          sub+=[i]
          if len(sub)==n: result+=[sub] ; sub=[]
      if sub: result+=[sub]
      return result
  </code></pre>
  
  <p>An example:</p>
  
  <p>If the list is:</p>
  
  <pre><code>[1,2,3,4,5,6,7,8]
  </code></pre>
  
  <p>And n is:</p>
  
  <pre><code>3
  </code></pre>
  
  <p>Return:</p>
  
  <pre><code>[[1, 2, 3], [4, 5, 6], [7, 8]]
  </code></pre>
  
  <p>Is there a more eloquent / concise way?</p>
  
  <p>An aside, what is preferred when appending lists to lists (in the context above):</p>
  
  <pre><code>list1+=[list2]
  </code></pre>
  
  <p>Or:</p>
  
  <pre><code>list1.append(list2)
  </code></pre>
  
  <p>Given that (according to Summerfeild's 'Programming in Python 3') they are the same?</p>
  
  <p>Thanks.</p>