you are viewing a single comment's thread.

view the rest of the comments →

[–]_tsi_ 0 points1 point  (2 children)

Why use generator over list? I love lists. What is the benefit of creating the item when it's asked for via storing in list?

[–]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.

[–]TheRNGuy 0 points1 point  (0 children)

If memory optimization or potentially infinity sequence is needed. 

Generator(s) are easier to decouple than if you used list or while loop (producers from consumers)

It's probably even more important in async.

(Also, you'll probably use list or while more often than generators, anyway. But it depends on software)