My code is as follows:
nums = [1,2,3,4,5,6,7,8,9,10]
k = 3
k = k % len(nums)
nums[:] = nums[-k:] + nums[:-k]
Does [ : ] create a copy of the list?
In that case, is extra memory allocated if I use this code? Is it O(1)?
My code is as follows:
nums = [1,2,3,4,5,6,7,8,9,10]
k = 3
k = k % len(nums)
nums[:] = nums[-k:] + nums[:-k]
Does [ : ] create a copy of the list?
In that case, is extra memory allocated if I use this code? Is it O(1)?
[:]
does create a copy.
# data
x = [1, 2, 3]
y = x[:]
# changing x value
x[0] = 2
# checking out the results
print(x, y)
[2,2,3] [1,2,3]
Check out Python Slice Assignment Memory Usage