title: How does the list comprehension to flatten a python list work?
body: <p>I recently looked for a way to flatten a nested python list, like this: [[1,2,3],[4,5,6]], into this: [1,2,3,4,5,6].</p>
  
  <p>Stackoverflow was helpful as ever and I found <a href="https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python">a post</a> with this ingenious list comprehension:</p>
  
  <pre><code>l = [[1,2,3],[4,5,6]]
  flattened_l = [item for sublist in l for item in sublist]
  </code></pre>
  
  <p>I thought I understood how list comprehensions work, but apparently I haven't got the faintest idea.  What puzzles me most is that besides the comprehension above, this also runs (although it doesn't give the same result):</p>
  
  <pre><code>exactly_the_same_as_l = [item for item in sublist for sublist in l]
  </code></pre>
  
  <p>Can someone explain how python interprets these things? Based on the second comprension, I would expect that python interprets it back to front, but apparently that is not always the case. If it were, the first comprehension should throw an error, because 'sublist' does not exist. My mind is completely warped, help!</p>