title: Weird behaviour of generator unpacking in Python
body: <p>I am currently trying to be more familiar with iterators in Python, and I encountered some weird behaviour. Essentially, I get the wrong behaviour with a generator comprehension, but the correct behaviour with a list comprehension. </p>
  
  <p><strong>Let me start by explaining what I try to do, and then what behaviour I get.</strong><br>
  Imagine having a dictionary of iterables, e.g.</p>
  
  <pre><code>d = {'a': [1, 2, 3], 'b': [4, 5]}
  </code></pre>
  
  <p>What I want is to have a list of dictionaries with all possible combinations of the iterable. For the first example, this would be</p>
  
  <pre><code>l = [
          {'a': 1, 'b': 4},
          {'a': 1, 'b': 5},
          {'a': 2, 'b': 4},
          {'a': 2, 'b': 5},
          {'a': 3, 'b': 4},
          {'a': 3, 'b': 5},
      ]   
  </code></pre>
  
  <p>To do this, I created this generator:</p>
  
  <pre><code>def dict_value_iterator(d):
      for k, v in d.items():         
          yield ((k, vi) for vi in v)
  </code></pre>
  
  <p>The idea was to run the following code to get the wanted result</p>
  
  <pre><code>def get_all_dicts(d):
      return map(dict, *itertools.product(dict_value_iterator(d)))
  </code></pre>
  
  <p><strong>Now, for the strange behaviour.</strong><br>
  To test that the <code>dict_value_iterator</code> generator indeed did what I hoped it would, I ran the following code:</p>
  
  <pre><code>for i in dict_value_iterator(d):
      print(list(i))
  </code></pre>
  
  <p>which indeed does what I hoped it would, namely print out the following:</p>
  
  <pre><code>[('a', 1), ('a', 2), ('a', 3)]
  [('b', 4), ('b', 5)]
  </code></pre>
  
  <p>However, when I run the following code</p>
  
  <pre><code>def test_unpacking(*args):
      for a in args:
          print(list(a))
  test_unpacking(*dict_value_iterator(d))
  </code></pre>
  
  <p>I get the output</p>
  
  <pre><code>[('b', 1), ('b', 2), ('b', 3)]
  [('b', 4), ('b', 5)]
  </code></pre>
  
  <p>This makes little to no sense for me, why does iterator unpacking change anything. </p>
  
  <p><strong>Final note.</strong><br>
  The way I found it was by running the  <code>get_all_dicts</code> function on d, which resulted in the following output</p>
  
  <pre><code>[{'b': 4}, {'b': 5}, {'b': 4}, {'b': 5}, {'b': 4}, {'b': 5}]
  </code></pre>
  
  <p>However, when I modify the <code>dict_value_iterator</code> as follows</p>
  
  <pre><code>def dict_value_iterator(d):
      for k, v in d.items():         
          yield ((k, vi) for vi in v)
  </code></pre>
  
  <p>I get this output</p>
  
  <pre><code>[{'a': 1, 'b': 4},
   {'a': 1, 'b': 5},
   {'a': 2, 'b': 4},
   {'a': 2, 'b': 5},
   {'a': 3, 'b': 4},
   {'a': 3, 'b': 5}]
  </code></pre>
  
  <p>which is what I want.</p>