Generator expression using more memory than list comprehension, what gives
I thought generators were always the memory-saving option. But sum(x for x in range(10_000_000)) seemed to use more RAM than sum([x for x in range(10_000_000)]) in my quick tracemalloc test on Python 3.12. Did I mess up the measurement, or is there something about generators I'm fundamentally missing? I was quite confident going into this and now I'm not confident at all.
4 replies