When I iterate through the values of list1 from start to stop, as in:
for value in list1[start:stop]:
....
Does python first copy that part of the list (as is done when doing list2 = list1[:])? This could get very expensive for large lists!
If it doesn’t copy it in the above example, does that always hold true? I need to do the following sort of loop, very often, on large sections of (very) large lists:
for index, value in enumerate(list1[start:stop], start):
....
list1[start:stop]creates a new list, period. This is always the case, regardless of whether you’re iterating over the result directly or have a function in between or use it in any other context (you’d need a moderately static language, or sophisticated type inference, to optimize even for simple instances of the first case).Note that this is independent from the iteration! The iteration itself does not copy, and the list slicing copies even if you throw the result away.
It only copies pointers though, so if you’re always taking very small sublists, you probably wouldn’t notice any difference. If the sublists are larger, you could either iterate over indices (
[x]range) or useitertools.islice. The latter would have to skip overstartitems first though, so you may pay a hefty time penality for the memory savings. The former is ugly, but most efficent asymptomically.