0

I had a code snippet behaving weirdly; I made another to illustrate the problem. I think the two snippets should yield identical results but don't :

alphas = np.arange(5)
args = zip(np.arange(5), np.arange(5))

output = [np.mean([ a+b for a, b in args]) for _ in alphas]

yielding

[4.0, nan, nan, nan, nan]

and

alphas = np.arange(5)
args = zip(np.arange(5), np.arange(5))
args = list(args)  # <- this is the only difference

output = [np.mean([ a+b for a, b in args]) for _ in alphas]

yielding

[4.0, 4.0, 4.0, 4.0, 4.0]

The only difference is that the generator is converted to a list inside the list comprehension. Does anyone understand the underlying mechanism that makes the results different ?

Prune
  • 76,765
  • 14
  • 60
  • 81

1 Answers1

2

A generator produces the output sequence only once. Your first expression uses the entire sequence on the out for's first iteration. When _ advances from 0 to 1 and cycles around to iterate through args again, there's nothing left to yield, and you have an empty return. This results in nan for the mean value.

In your second example, args is a static list; you can iterate through that as many times as you like.

Prune
  • 76,765
  • 14
  • 60
  • 81