title: Nesting generator expressions in the argument list for a python function call
body: <p>I like to use the following idiom for combining lists together, sometimes:</p>
  
  <pre><code>&gt;&gt;&gt; list(itertools.chain(*[[(e, n) for e in l] for n, l in (('a', [1,2]),('b',[3,4]))]))
  [(1, 'a'), (2, 'a'), (3, 'b'), (4, 'b')]
  </code></pre>
  
  <p>(I know there are easier ways to get this particular result, but it comes comes in handy when you want to iterate over the elements in lists of lists of lists, or something like that.  The trouble is, when you use generator expressions, this becomes error prone.  E.g.</p>
  
  <pre><code>&gt;&gt;&gt; list(itertools.chain(*(((e, n) for e in l) for n, l in (('a', [1,2]),('b',[3,4])))))
  [(1, 'b'), (2, 'b'), (3, 'b'), (4, 'b')]
  </code></pre>
  
  <p>What's happening here is that the inner generator expressions get passed as arguments to <code>itertools.chain</code>, so at the the time they're evaluated, the outer generator expression has finished, and <code>n</code> is fixed at its final value, <code>'b'</code>. I'm wondering whether anyone has thought of ways to avoid this kind of error, beyond "don't do that."</p>