Here's the remove_adjacent challenge and my solution so far:
# D. Given a list of numbers, return a list where
# all adjacent == elements have been reduced to a single element,
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
# modify the passed in list.
def remove_adjacent(nums):
for n in nums:
i = nums.index(n)
if nums[i]==nums[i+1]:
nums = nums.remove(n)
return nums
What I am trying to do is to iterate over the list 'nums' checking if each element is the same as the next one. If it is, the first appearance is removed so that the for loop will still check the next one.
The problem is the line 'i = nums.index(n)' is throwing off this error: AttributeError: 'NoneType' object has no attribute 'index'
If 'nums' is defined as a list, why can't I use 'nums.index(n)' to get the index for each item n?
Thanks for any help.