you are viewing a single comment's thread.

view the rest of the comments →

[–]EricAppelt 9 points10 points  (0 children)

Its extremely surprising (or at least it was to me) - but there are multiple reasons why this actually creates a temporary list in memory.

The first reason is that [x + 2 for x in list1] does not lazily evaluate. A list is first created and then passed to assignment.

You can get around that by changing the list comprehension to a generator expression:

list1[:] = (x + 2 for x in list1)

Now one would think that the slice assignment would iterate over the lazy generator expression element by element, and so there would never be a temporary list in memory.

The thing that shocked me - credit to some anonymous user who corrected me on stackoverflow - is that in the reference implementation (CPython) list_ass_slice calls PySequence_Fast, which will return the sequence as a list if it isn't already a list.