If you use large static iterables in Python, a list may not be an optimal choice, especially in memory-constrained applications. A list stores the entire collection in memory. However, a generator computes and loads a single element at a time ONLY when it is required. This saves both memory and object creation time.
Save Memory with Python Generators
Save Memory with Python Generators
Save Memory with Python Generators
If you use large static iterables in Python, a list may not be an optimal choice, especially in memory-constrained applications. A list stores the entire collection in memory. However, a generator computes and loads a single element at a time ONLY when it is required. This saves both memory and object creation time.