I’m writing a generator function. I want to know if there’s a better (read: more pythonic, ideally with a list comprehension) way to implement something like this:
generator = gen()
captures = []
for _ in xrange(x):
foo = next(generator)
directories.append(foo['name'])
yield foo
The key here is that I don’t want to capture the WHOLE yield- the dictionary returned by gen() is large, which is why I’m using a generator. I do need to capture all of the ‘name’s, though. I feel like there’s a way to do this with a list comprehension, but I’m just not seeing it. Thoughts?
There is another / shorter way to do this, but I wouldn’t call it more Pythonic:
This takes advantage of the fact that
append, like all mutating methods in Python, always returnsNoneso.append(...) or foowill always evaluate tofoo.That way the whole dictionary is still the result of the generator expression, and you still get lazy evaluation, but the name still gets saved to the
directorieslist.You could also use this method in an explicit
forloop:or even just simplify your loop a bit:
as there is no reason to use an
xrangejust to iterate over the generator (unless you actually want to only iterate some known number of steps in).