Using Lists when dealing with large dataset is not always the best choice. As the data gets too big it will quickly consume your computer’s memory.
That’s when you should consider using Python’s Generators.
Let’s say you want to calculcate the sum of 10 000 items:
|
|
This would of course work, but the impact on memory would be very high. We can do the same thing with generators and then compare the sizes.
|
|
The key difference is that a generator computes the elements lazily, meaning it produces only one item at a time and only when asked for it.
Now let’s compare the sizes of both results:
|
|
Using List resulted in 87 632 bytes, while using Generator resulted only in 128 bytes. That surely is a difference that is worth considering.
I guess considering which one to use depends on the situation, but knowing about this great pythonic feature can save a lot of time and issues down the road.