Remove_duplicates: I don't understand the given solution

python

#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/removeduplicates?link_content_target=interstitial_lesson

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

```python

Replace this line with your code.

def remove_duplicates(inputlist):
    if inputlist == []:
        return []
    
 
    inputlist = sorted(inputlist)

    outputlist = [inputlist[0]]

    for i in inputlist:
        if i > outputlist[-1]:
            outputlist.append(i)
        
    return outputlist
print remove_duplicates([4,6,2,6,87,34])

what I do not understand here is if i > outputlist[-1]: part. From the piece of code outputlist = [inputlist[0]] we can see that outputlist is fixed - it has a value of first number in inputlist. That number doesn’t change so for ex. when we have two dupes after the first number in the inputlist it will not recognize it.
For example, if the sorted inputlist is [ 2,4,6,6,8,10] outputlist will give 2 all the time which means that duplicates will not be removed. What I mean is that outputlist[-1] always refers to the same value.
What is wrong here in my logic? I assume that given solution is correct.


#2

here is your flaw:

give we append() to outputlist it will not be 2 all the time, which we can prove with a simple print statement:

def remove_duplicates(inputlist):
    if inputlist == []:
        return []
    
 
    inputlist = sorted(inputlist)

    outputlist = [inputlist[0]]

    for i in inputlist:
        print outputlist, outputlist[-1]
        if i > outputlist[-1]:
            outputlist.append(i)
        
    return outputlist
print remove_duplicates([4,6,2,6,87,34])

#3

Side note: If you have more questions in the future, it’s good to know how to format your code in the post so that it’s easier for others to offer their help. You can refer how to format code here.

For the right advice or code review, share the lesson name, the webpage link, the step you are struggling on, also the code you tried and sometimes the error message that pops up. :thinking:
All of this information is really helpful for you to mull over and it introduces us to the problem so we can point you in the right direction in a quickly :handshake: :tada:


#4

First let’s make things clear and know what this code do exactly : the code just make another list that only append the “different elements”, the code sort the list to make all the similar elements next to each other , and since sorting method sort them in ascending order , so it uses ‘>’ operand to check for different element
and since that every new element is appended at the end of the list so comparing the current element to the last element is the right thing to do
Second the let’s look at the code to make sure that we know exactly how it does that

def remove_duplicates(inputlist):
    if inputlist == []:   
        return []
    
 
    inputlist = sorted(inputlist) #inputlist = [2,2,4,6,6,6,34,87]

    outputlist = [inputlist[0]] #outputlist = [2] this is inital value 
    index = 0
    for i in inputlist:              #let's see what each loop does on editor window
         
        print "In loop turn :%i"%index
        print "The list till now",outputlist
        print "The current element",i
        print "is %i > %i ?"%(i,outputlist[-1]) 
        index+=1

        if i > outputlist[-1]:     # by putting 'print' command here as stetim94 advised
            outputlist.append(i)
            

        
    return outputlist
print remove_duplicates([4,6,2,6,87,34,6,2]) #I adjusted it abit

Editor Window
In loop turn :0
The list till now [2]
The current element 2
is 2 > 2 ?
In loop turn :1
The list till now [2]
The current element 2
is 2 > 2 ?
In loop turn :2
The list till now [2]
The current element 4
is 4 > 2 ?
In loop turn :3
The list till now [2, 4]
The current element 6
is 6 > 4 ?
In loop turn :4
The list till now [2, 4, 6]
The current element 6
is 6 > 6 ?
In loop turn :5
The list till now [2, 4, 6]
The current element 6
is 6 > 6 ?
In loop turn :6
The list till now [2, 4, 6]
The current element 34
is 34 > 6 ?
In loop turn :7
The list till now [2, 4, 6, 34]
The current element 87
is 87 > 34 ?
[2, 4, 6, 34, 87]

I hope that explanation made the your point clear


#5

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