This should work for a transparent (albeit roundabout) solution:
def remove_adjacent(nums):
numstail = [i for i in range(0,len(nums))]
nums = nums + numstail
for i in nums:
if nums[i] == nums[i-1]:
del nums[i]
return nums[:-len(numstail)]
The logic is as follows:
- Create a tail-list equal to the length of the original list of numbers and append this to the end of the original list.
- Run a 'for-loop' that checks if a given element of nums is the same as the previous element. If so, delete it.
- Return the new nums list, with the necessary deletions, up to
len(numtails) index positions from the end of the list.
(numstail is defined to avoid indices being out of range for any length list)