You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generators are a powerful tool to save memory and improve performance. In general, they yield one value at a time and can be iterated over multiple times.
That's not true. After the first go-through the generator becomes empty.
Also... making this kind of generator from range is kinda a wacky idea. You can just use the range itself, which also acts like a generator and saves some additional memory.
And it can actually be iterated multiple times.
Also... making this kind of generator from range is kinda a wacky idea. You can just use the range itself, which also acts like a generator and saves some additional memory. And it can actually be iterated multiple times.
@SyberiaK in Python, range() is often compared to generators due to its low memory usage. While it behaves like a generator in some ways, it is not a true generator.
For instance, in the generator example (given below), when the for loop breaks at i == 3, the generator "remembers" that iteration and continues from 4 when you convert it to a list.
However, rangedoes not remember where you left off, leading to a reset-like behavior when evaluated again. This reduces efficiency in tasks like file reading, data collection and many others where on need to keep track.
While range() is memory-efficient, it lacks the advantages of a true generator. It behaves more like a lightweight list than a generator because it can be re-evaluated without remembering its state. One of the best explanation of range is given in this answer of SO.
That's not true. After the first go-through the generator becomes empty.
Also... making this kind of generator from
range
is kinda a wacky idea. You can just use the range itself, which also acts like a generator and saves some additional memory.And it can actually be iterated multiple times.
The text was updated successfully, but these errors were encountered: