Practice Makes Perfect 14/15 Can you explain the code?


#1

So I solved this with my code:

def remove_duplicates(lst):
newlst = []
for number in lst:
if number not in newlst:
newlst.append(number)
return newlst
print remove_duplicates([1, 1, 2, 2])

BUT…I wanted to see the Codecademy solution, and I’m having problems understanding whats actually happening in some part of the code. Can you help me understand?

The Codecademy code:

def remove_duplicates(inputlist):
if inputlist == []: <—If inputlist equals to a list then return list
return [] <—list is returned

inputlist = sorted(inputlist) <----we sort the list

outputlist = [inputlist[0]]   <------ outputlist is equal to 1 or a list with the integer 1 inside it 


for i in inputlist:
    if i > outputlist[-1]:  <----- Here I get confused...So if i, which is 1 to begin with, is bigger than outputlist[-1] ?
        outputlist.append(i)
    
return outputlist

print remove_duplicates([1, 1, 2, 2])

Im not quite getting the outputlist[-1] part…


#2

we can access values from the right-hand side of the list using negative indices:

print ['a', 'b', 'c'][-1] # output: c

#3

Ok so let say that we use my list [1 , 1, 2, 2]

We know that outputlist = [1] to begin with…

In the for loop, i is equal to 1 to begin with…

So…

IF 1 > 1?

Then nothing is appended and we go to the next i, which is 1 again

So…

IF 1 > 1

Nothing is appended again and we go to the next i, which is 2

So…

IF 2 > 1

i which is 2, is appended, so outputlist is now [1, 2]

And we go to the next and final number, which is 2, and now we compare it to that last number in outputlist, which is now 2, so…

IF 2 > 2

Nothing is appended and since there are no more items, we return outputlist, which is [1, 2]

Did I get it right?


#4

yep, absolutely spot on :slight_smile: Nicely done :slight_smile:


#5

Awesome!!! :smiley:

I wish that Codecademy showed us little tricks like that, I didnt know we could access values from the righthand side of a list like that


#6

But I was wondering about something, is it really necessary with this:

I mean, we already know its a list, we called the fucntion with the [1, 1, 2, 2]…


#7

this checks if list is empty, if so, return empty list

otherwise, you could get an index error here:

outputlist = [inputlist[0]] 

if the list is empty, there is nothing at index 0.


#8

I see that I get the error when I remove that piece of code:

remove_duplicates([]) resulted in an error: list index out of range

But I still dont get why we get the error, because the output gives me the exact same result with or without that piece of code, and on top of that, when we call the function we call it with our list [1, 1, 2, 2]. And our list is not emtpy, so…? Whats the reason for checking if its empty or not, when we already know its not empty? Is this piece of code necessary, just in case somebody else uses that piece of code and decides to call the function with an empty list?


#9

if you add this:

remove_duplicates([])

at the very end of your script, you will get an error

Your program shouldn’t crash on an empty list.


#10

Yes I see that, because first we call the function with my list, it gets returned without the duplicates and then it calls the fucntion again with an empty list and returns nothing…and error yes, but I want to know why that piece of code is necessary is it maybe because of this:

Should I always have other people in mind when I write code, you know put those extra lines of code so that maybe other people can call the function with some other values or no values at all…? Is that what it is or…?


#11

In this specific case, the function should be able to handle all lists, including empty ones.

Yea, normally you use functions to make a program for a client. Then the program should be able to handle all lists, given you don’t know what the client/user might enter.


#12

So we should actually do this with all of our functions, shouldn’t we?

Right, exactly


#13

it depends on the function, for now, the exercise sets the condition of what the function should handle


#14

Yes because in my code, I didn’t have a problem…

def remove_duplicates(lst):
newlst = []
for number in lst:
if number not in newlst:
newlst.append(number)
return newlst
print remove_duplicates([1, 1, 2, 2])

But when I looked at the Codecademy solution, I was a bit confused by that piece of code…I would’ve never thought to make sure that my function should be able to return an empty list too…but I’ll keep it in the back of my head now, just in case I get some issues with my code in the future…

But I see now, why I dont get an error whith my code…because if my function was called with an empty list, it wouldnt be a problem, because my function will be returned with an epmty list that I already have created newlst = [] :wink:


#15

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