Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: If an iterator is an expression, is it calculated every time?

Take the following example:

>>> for item in [i * 2 for i in range(1, 10)]:
    print item


2
4
6
8
10
12
14
16
18

Is [i * 2 for i in range(1, 10)] computed every time through the loop, or just once and stored? (Also, what is the proper name for that part of the expression?)

One reason I would want to do this is that I only want the results of that list comprehension to be available in the loop.

like image 698
Nick Heiner Avatar asked Dec 23 '22 01:12

Nick Heiner


2 Answers

A good translation of for i in <whatever>: <loopbody>, showing exactly what it does for any <whatever> and any <loopbody>:

_aux = iter(<whatever>)
while True:
  try: i = next(_aux)
  except StopIteration: break
  <loopbody>

except that the pseudo-variable I have here named _aux actually remains unnamed.

So, <whatever> always gets evaluated just once (to get an iter() from it) and the resulting iterator is nexted until it runs out (unless there's some break in the <loopbody>).

With a listcomp, as you've used, the evaluation produces a list object (which in your code sample remains unnamed). In the very similar code:

for item in (i * 2 for i in range(1, 10)): ...

using a genexp rather than the listcomp (syntactically, round parentheses instead of the listcomp's square brackets), it's the next() that actually does most of the work (advancing i and doubling it), instead of lumping all work at construction time -- this takes up less temporary memory, and may save time if the loop body's reasonably likely to break out early, but except in such special conditions (very tight memory or likely early loop termination) a listcomp may typically be (by a wee little bit) faster.

like image 159
Alex Martelli Avatar answered Dec 24 '22 16:12

Alex Martelli


All members of the expression list are calculated once, and then iterated over.

In Python 2.x, the variable used in a LC does leak out into the parent scope, but since the LC has already been evaluated, the only value available is the one used to generate the final element in the resultant list.

like image 45
Ignacio Vazquez-Abrams Avatar answered Dec 24 '22 15:12

Ignacio Vazquez-Abrams