Is there some way to make a notebook automatically listify generators? When I’m interactively exploring an idea, I’ll often end up with something like one of these:
<generator object PageElement.stripped_strings at 0x10dbd7d30>
<itertools.chain at 0x10e5a7430>
I almost universally want to see the actual elements so I have to go back and add list() to the cell and re-execute it. It would be really nice if this could happen automatically.
Unfortunately, any automatic behavior would have the side-effect of consuming the generator.
For the interactive case, pointers to the history of previous cells are available in a few magic variables: Out[n] and _ (and __/___ for deeper history).
In[1]: x = (a for a in range(3))
Out[1]: x
If the data really doesn’t matter, the next cell could be:
In[2]: [*_] # or [*Out[1]]
Out[2]: [0, 1, 2]
These really fall over with out-of-band execution: reading that generator again would return the empty list. If the data are important, it’s likely better to do the explicit list() call, or something more reproducible like sorted() or set().