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 ?