Python: does iterate through "list [a: b]" the first copy of this part of the list (which can be expensive)?

When I repeat the values โ€‹โ€‹of list1 from start to stop , as in:

 for value in list1[start:stop]: .... 

Does python first copy this part of the list (how is this done when doing list2 = list1[:] )? This can become very expensive for large lists!

If he does not copy it in the above example, is this always true? I very often have to do the following kind of loop on large sections of (very) large lists:

 for index, value in enumerate(list1[start:stop], start): .... 
+6
source share
3 answers

list1[start:stop] creates a new list, period. This is always the case, regardless of whether you directly execute the result or execute the function between them or use it in any other context (you will need a moderately static language or a complex type of output for optimization even for simple instances of the first case).

Note that this does not depend on iteration ! The iteration itself is not copied, and the list cuts the copies, even if you throw away the result.

It only copies pointers, so if you always accept very small sublists, you probably won't notice any difference. If there are more sublists, you can either itertools.islice over the indexes ( [x]range ) or use itertools.islice . The latter must first skip the start points, so you can pay a hefty temporary penalty for saving memory. The first is ugly, but most effectively asymptomatic.

+8
source

Yes.

The expression list1[start:stop] creates a new list containing items in the specified range. However, it does not copy the actual data, but just a bunch of pointers, so if the list is not too long, it usually does not have overhead.

+2
source

Instead of asking this question, you could simply test it using the magic identifier method

 >>> x=[1,2,3,4] >>> id(x[1]) 4971620 >>> id(x[1:][0]) #same as the original list 4971620 >>> id(x[2:3]) 44327400 >>> id(x) 44408224 >>> 

x[2:3] actually creates a new list, but the items are still in the original list.

+1
source

Source: https://habr.com/ru/post/904243/


All Articles