When a list is sliced, are the references to its contents copied from the original list? I can imagine that this may not be necessary, but I read the opposite (mentioned in passing).
This question matters for instance for the following idiom, in the case of a very long my_list
:
for (first_elmt, second_elmt) in itertools.izip(my_list[:-1], my_list[1:]):
…
A copy would use up both memory and, presumably, some time. I compared to looping over the index of first_elmt
with xrange()
, on a list of 100 million integers. The slicing approach is actually 20% faster, but does seem to copy references (the system time is longer). Is this indeed the case?
PS: I now realize that it is quite natural that slices copy the references: if the original list is modified, the slice does not change, so it is easier to have the implementation of the slice copy the references of the original list. A pointer to the CPython implementation would be interesting, though.