If I were to do this in a very long process, when a lot of data was consumed, would this “list” continue to use memory until the holidays?
That's right.
When will the garbage collector require the return of memory? After the volume that is built in this list is lost?
CPython uses reference counting, so this is the most likely case. Other implementations work differently, so don't count on it.
Thanks to Karl, pointing out that thanks to the complex memory management mechanisms used by CPython, this does not mean that memory will immediately return to the OS after that.
I don't know how the python garbage collector works, but I would venture to say that this list will exist until the last one is called on all_stuff.
I don't think such a garbage collector works like this. They are usually tag-and-sweep, so it may take quite some time before the list is garbage collected.
This is a generic pattern found in the list-comp statement.
Absolutely not. The fact is that you iterate over the list in order to do something with each element ( do side effects are caused for it). In all List-comp HOWTO examples, the list is repeated to create a new list based on the elements of the old. Consider an example:
# list comp, creates the list [0,1,2,3,4,5,6,7,8,9] [i for i in range(10)]
Perhaps you will agree that this cycle is completely meaningless, since it does nothing, contrary to the understanding that builds the list. In your example, this is the opposite: Understanding is completely pointless because you don't need a list! More information on this issue can be found in the related question.
By the way, if you really want to write this loop in one line, use a consumer generator, for example deque.extend . This will be slightly slower than the original for loop in this simple example:
>>> from collections import deque >>> consume = deque(maxlen=0).extend >>> consume(do(stuff) for stuff in all_stuff)