Remove duplicates


if lst[i+1]==lst[i]
SyntaxError: invalid syntax

why it is invalid?

def remove_duplicates(lst):
    for i in range(0,len(lst)-1):
        if lst[i+1]==lst[i]
    return lst2

Replace this line with your code.


It's invalid because you're missing a colon at the end of that line to start the if statement.
I don't know if the logic is valid, but it looks fine to me.


ooh yes..thank you..:wink:


No problemo


okay , now what is the problem, can you please tell.

def remove_duplicates(lst):
    for i in range(0,len(lst2)-1):
      for j in range(i+1,len(lst2)-1):
          if lst2[i]==lst2[j]:
    return lst2


What's the error you're getting?
I honestly don't want to go into the logic and try to figure out what's wrong because i will most likely get it wrong.




will not make a copy of the list, it will make a variable which refers to the same list.

the problem is that remove is dreadful approach to solve this problem. check the hint of the exercise, it gives a good hint for a good approach


Your remove_duplicates modifies the list that was passed to it. [4, 5, 5, 4] was modified to [4, 5, 4]. Perhaps you should make a copy first!
This is the error , ok i will be using what has been given in the hint .
But can you tell me why this error is coming, when it should not.


def remove_duplicates(lst):
# lst2=lst
for i in range(0,len(lst)-1):
for j in range(i+1,len(lst)-1):
if lst[j]==lst[i]:

return lst


And it is with hint that was given:

def remove_duplicates(lst):
for i in lst:
if i not in lst2:

return lst2


you didn't make a copy of your list. That is where the error message come from.

and remove is still a dreadful approach to solve this problem, because of index shift.

with the hint you seemed to have solved it fine :slight_smile:


Why do i need to make a copy?


Yes with that hint i solved the problem.. :wink:


From the hint ...

The easiest way to approach this problem is to create a new list in your function ...

  • Create a new empty list
  • Loop through the original list, and append to the new list any items from that list that are not already in the new list.
  • After the loop, return the new list.


I have done that way, output is coming. My question is why i have to make the copy or a new list, why cant i alter the same list and return that.


because the exercise says so:

  1. Do not modify the list you take as input! Instead, return a new list.


Okay , i thought it is in python ..that this way things work.. thanks :slight_smile:


From the hint ...

Also, note that destructively modifying a list while you are looping through it is bad practice and will likely lead to bugs ...

It can lead to bugs because the user of the function may want to preserve the original list as it was, for some purpose, even as the user utilizes the modified version of the list for a different purpose. Sometimes we need both an original and a modified version of an object.


Thank you once again..:slight_smile:


It is essential for there to be an understanding between the author of a function and all users of the function. To facilitate that understanding, everything the function does should be documented. In the current case, Codecademy is one of the users of the remove_duplicates function, since they call it in order to test it. They have stipulated that it should not modify the original list. In other cases, we can have functions that do modify the original object, provided that this is revealed in the documentation.

An example of a Python function that leaves an original object intact and returns a modified copy is the built in sorted function. See Python 3: sorted.

An example of a Python function that modifies the original object is the sort method. See Python 3: list: sort. Note that since the sort function is a method, the syntax for calling it is different from that of calling the sorted function.

In both cases, the user can decide whether or not to preserve two separate copies of the object, one being the original, and the other being the modified version. The important issue is to prevent a misunderstanding between the author and the users of the function.