Does Python Copy References To Objects When Slicing A List?
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
Solution 1:
Slicing will copy the references. If you have a list of 100 million things:
l = [object() for i in xrange(100000000)]
and you make a slice:
l2 = l[:-1]
l2
will have its own backing array of 99,999,999 pointers, rather than sharing l
's array. However, the objects those pointers refer to are not copied:
>>> l2[0] is l[0]
True
If you want to iterate over overlapping pairs of elements of a list without making a copy, you can zip
the list with an iterator that has been advanced one position:
second_items = iter(l)
next(second_items, None) # Avoid exception on empty inputfor thing1, thing2 in itertools.izip(l, second_items):
whatever()
This takes advantage of the fact that zip
stops when any input iterator stops. This can be extended to cases where you're already working with an iterator using itertools.tee
i1, i2 = itertools.tee(iterator)
next(i2, None)
for thing1, thing2 in itertools.izip(i1, i2):
whatever()
Solution 2:
Yes, slicing DO copy references, in fact, it is an idiom to make copy of a list this way: newlst = lst[:]
.
Post a Comment for "Does Python Copy References To Objects When Slicing A List?"