Remove_duplicates doesn't get code validation


#1




https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096


I get this error message while I'm running my code:
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!


But I'd tested that in both idle 3.5 and idle 2.7 python shells. It works fine. The output is:
[4, 5]
Maybe it's a bug in code validation procedure?


def remove_duplicates(glist):
    uniqs = []
    prev = None
    sorted_glist = glist
    sorted_glist.sort()
    for curr in sorted_glist:
        #print(prev)
        #print(curr)
        if curr != prev:
            uniqs.append(curr)
        prev = curr
    return uniqs

print(remove_duplicates([4, 5, 5, 4]))


#2

This is my solution

def remove_duplicates(a_list):
    new_list = []
    for i in a_list:
        if i not in new_list:
            new_list.append(i)
    return new_list
        
a = [1,2,4,6,4,2,3]

print remove_duplicates(a)

The prev and sorted_glist variables in your code are unnecessary


#3

They told me do not modify the list I take as input. So I've tried to create a copy. But somehow it still modifies. I'd added

print(glist)

before return and got [4, 4, 5, 5]. Why? Isn't sorted_glist a copy of a glist?


#4

OK. It's not a bug. It's my misunderstanding of some memory stuff... I had to write

sorted_glist = glist[:]

to make a real copy of my input list.


#5

You don't have to sort the argu list
Because you are assigning "sorted_glist" to glist. You are assigning the same unsorted list to the variable and sorting that variable later, without storing the sorted variable.

I'd write your code like this:

sorted_glist = sorted(glist)

I don't understand why you tried to compare every item in the list agaisnt none, since this:

will evaluate to true, EVERY SINGLE ITEM in the list is not a None, and even if you used

if curr == prev:

the condition would return False, and not execute the code inside of it.

which would be changed with

if you had indented it properly inside the if condition, as so:

but that resets as soon as the next number is not equal to curr, and if the consecutive numbers were not equal, they would be eventually appended to your new list.


#6

That is the same as saying sorted_glist = glist

:expressionless:

Since [:] means the entire list from the 0 index to the last index, in steps of 1 by default.


#7

I fixed and cleaned your code up down to this, with the unnecessary extra sorting:

def remove_duplicates(glist):
    uniqs = []
    sorted_glist = sorted(glist)
    for curr in sorted_glist:
        if curr not in uniqs:
            uniqs.append(curr)
    return uniqs

b = [4, 5, 5, 4]
print remove_duplicates(b)

This is how it should look for this exercise, but could be done differently outside of Codecademy:

def remove_duplicates(glist):
    uniqs = []
    for curr in glist:
        if curr not in uniqs:
            uniqs.append(curr)
    return uniqs

b = [4, 5, 5, 4]
print remove_duplicates(b)

#8

I didn't have to. But I did. I just wanted to understand what went wrong.

Yes, it's much better. I didn't know about that function. Thanks :slight_smile:

Not every but first only. After first iteration prev changes.


#9

Actually not. As said in the official Python documentation, shallow copies of list can be made by assigning a slice of the entire list, for example,

copied_list = original_list[:]

As I've understood,

sorted_glist = glist

makes another reference to the same area of memory there original glist located in. That's why glist also modifying every time I modify sorted_glist.


#10

I wasnt aware of the [:] behaviour of making

.

I gave your PREV a chance, and ended up with this code:

def remove_duplicates(glist):
    prev = None
    uniqs = []
    sorted_glist = sorted(glist)
    for curr in sorted_glist:
        if curr not in uniqs:
            uniqs.append(curr)
            prev = curr
    return uniqs
b = [4, 5, 5, 4]
print remove_duplicates(b)

Since I realized, that when every item is sorted in ascending order, repeating items wont change the prev variable, and this is how the look with print statements would look like, for debugging purposes:

def remove_duplicates(glist):
    prev = None
    uniqs = []
    sorted_glist = sorted(glist)
    print sorted_glist
    for curr in sorted_glist:
        if curr not in uniqs:
            uniqs.append(curr)
            prev = curr
            print prev
            print uniqs
    return uniqs
b = [4, 5, 5, 4]
print remove_duplicates(b)

#11

My code is already working fine after [:] addition. Thank you for your help. I like your first solution, it much more elegant than mine. :slight_smile:


#12

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