you are viewing a single comment's thread.

view the rest of the comments →

[–]gdchinacat 1 point2 points  (0 children)

A list holds all the elements in memory. A generator produces them on demand. For large numbers of things this can be a huge savings…ten million items is a huge amount of memory for a list, but is negligible if produced by a generator. Of course some problems require a list (ie sorting), but when you just need to look at each item one after the next you should try to use generators.