Generator expressions

A list comprehension uses square brackets and builds the whole list now: [n * n for n in nums]. Swap them for parentheses and you get a generator expression: (n * n for n in nums). It builds nothing up front. It makes each value only when asked.

That matters for memory. The list of a million squares takes megabytes; the generator takes about 200 bytes, whatever the size. When a function consumes the values once, like sum, max, any, or "".join, you can drop the extra brackets: sum(n * n for n in nums).

Use a list when you need to loop more than once, index into it, or know its length. Use a generator when you go through the values once, especially with any or all, which stop early.

Interviewers like sum(1 for x in items if test(x)) to count matches without building a list.

Run it, then change limit to 10 and compare the sizes again.