I found this question that asks for in-place modification of an array, so that all zeros are moved the end of the array and the remaining order of non-zero elements is maintained. In-place, according to the problem statement, means without making a copy of the original array. (This is taken from Leetcode and can be found as #283, Move Zeroes)
An example input and output would be, [0,1,0,13,12] becomes [1,13,12,0,0]. One simple solution I saw is:
for num in nums:
if num == 0:
nums.remove(num)
nums.append(0)
The solution is clear and easy to follow, so I get that it is doing what it is supposed to.
However, I am not fully clear/sold on the in-place part, because I am not sure how remove works in the background. Does remove internally make a copy of the array to remove the specified element - how does it work? Using this notion of "in-place", is my initial solution below considered in-place (since it does not make any copies of nums, but rather modifies the original version of nums)?
indices = []
for en, num in enumerate(nums): # get the index of all elements that are 0
if num == 0:
indices.append(en)
for en, i in enumerate(indices):
new_i = i-en # use the index, accounting for the change in length of the array from removing zeros
nums = nums[:new_i] + nums[new_i+1:] # remove the zero element
nums = nums + [0] * len(indices) # add back the appropriate number of zeros at the end

for num in nums[:]:, then you're safe to modify the original list.for i in range(len(nums))and access the array by indices.