Remove Duplicates


#1



My code works just fine and I get the correct output but the code academy part gives me the error "Oops, try again. Your remove_duplicates modifies the list that was passed to it. [4, 5, 5, 4] was modified to [4, 4, 5, 5]. Perhaps you should make a copy first!" even though when I print the result of the my function it gives me [4,5]

I realize my code isn't the most streamlined, but it does work so I am frustrated that code academy feels otherwise.
Thoughts?


def remove_duplicates(nums):
    nums.sort()
    singles=[]
    x=len(nums)
    i=1
    for num in nums:
        if num == nums[x-1]:
           if num not in singles:
                singles.append(num) 
        elif num != nums[i]:
            if num not in singles:
                singles.append(num)
        i=i+1
    
    print singles 
    return singles
    
remove_duplicates([4,5,5,4]).


#2

This seems like a reasonable step, except the SCT is expecting the order to be preserved.

That is the only if statement needed.

Neither variable is needed.


#3

If it is expecting the order to be preserved, then why is this in the
instructions: The order in which you present your output does not matter.
So returning [1,2,3] is the same as returning [3,1,2]?


#4

Never saw that before. When no link is provided to the lesson it is a real pain to track down the exact lesson. That leaves us guessing. A while back there was a member whose return value was not what was expected and changing the order seems to have fixed that. Unless it was something entirely different.

Anyway, if you want help with an exercise, please post a link.


#5

Never mind, I was taking the longest possible way around the barn lol


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.