Compare list and generator memory use without mistaking object size for total memory.
What You Will Learn
Lists retain their elements, while generators produce values on demand. sys.getsizeof reports only the shallow size of an object, so it does not calculate the complete memory cost of nested values or the whole process. Compare peak memory with tracemalloc when a realistic measurement is needed.
Many iterator tools are lazy. Creating the iterator does not necessarily perform the work; advancing it can consume the original source.
Step-by-Step Example
The example below makes the behavior visible with small input values that you can change safely.
import sys
numbers = [value ** 2 for value in range(10000)]
stream = (value ** 2 for value in range(10000))
print(len(numbers))
print(sys.getsizeof(numbers) > sys.getsizeof(stream))
print(next(stream))
How the Example Works
- The imports provide the standard-library tools used in the demonstration: sys.
- The variable numbers holds the result of [value ** 2 for value in range(10000)]. Later lines use that value or inspect how it changes.
- The variable stream holds the result of (value ** 2 for value in range(10000)). Later lines use that value or inspect how it changes.
- The print() calls make the return values or state visible so you can compare them with the explanation and check your prediction.
Expected output from the example:
10000
True
0
When You Would Use This
This pattern is useful in readers for CSV files, event streams, or paginated API results, where collecting everything in memory is unnecessary. Because iterator state advances when values are consumed, decide whether you need a reusable collection or a one-time pass. Keep files and connections open until their lazy consumers finish.
Practice: Modify the Code
Use tracemalloc to compare peak memory while summing both versions, without printing 10,000 values.
Verify the changed output against the behavior described above, including the relevant invalid or boundary case.
For complete parameters and version details, consult the official Python documentation.
Common Beginner Mistakes
- Interpreting sys.getsizeof(generator) as the entire cost of processing all future values.
- Assuming generators are always faster than lists in CPU time.
Key Takeaways
- Compare list and generator memory use without mistaking object size for total memory.
- Lists retain their elements, while generators produce values on demand. sys.getsizeof reports only the shallow size of an object, so it does not calculate the complete memory cost of nested values or the whole process.
- Apply this operation only after checking the assumptions and input conditions shown in the example.