Remove_duplicates : unable to debug code [SOLVED]



.remove() is a very difficult approach, not sure if you want to debug it.

Can you please share your code in such a way that i can actually run it?

What do you specifically need help with when it comes to debugging? Are you sure you want to go for .remove() approach? I would advise against it.


You might want to re-read the instructions, specifically:

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

As @stetim94 said, .remove() can be problematic which is why the lesson provides the specific instruction above to nudge you towards a more straightforward solution. I noticed you’ve also expanded the “Hint” text, which explains specifically why your approach of using .remove() is a bad idea.


i am sure i wanna go with .remove() approach, should i just copy and paste the code here?


But why? If you really want something while you get advise against it, you must have a danm good reason

and i can’t think of a single good reason, i might have phrased it as a question, but in truth it wasn’t a question :wink:


that was just a suggestion , i dont want to take it . i want to know where i am going wrong, if you notice the terminal window , it is going in the right direction until i dont know what happens , i cant find the answer as to why the index or where the index goes out of range.


because while i was thinking of a way for solving this problem, this was the first that came to my mind and i found it pretty easy and it is actually going fine if you notice the terminal window ( in the first run there are three 1s and in the second run there are two) but after that it just goes out of range but where exactly and how?


but i was suggested for a very good reason. Trust me, i have done it, and its absolutely not easy

I need to run the code myself, so i can fiddle with it.

Its the opposite of easy. I recently read a good statement: A great programmer looks at the different solutions and then implements the best one. Its good advise :wink:


it sure is a good advice but a great programmer would still know how to solve this with any particular approach and then i still want to take this approach , i am not here making code for somebody who paid me , i am here to learn everything i can, if my approach is logically correct which it is , then i will try my best to solve it , the code is just around 5 lines , you can write as it is under 2 minutes maybe faster or i can just copy it here.
now the question is , would you please help me with this particular approach? i dont want to waste time discussing whether its the right approach or not, i just want to solve this using append method


The lesson here should be not to pursue an approach which you know to be inefficient and difficult to solve

i was waiting for you too copy paste your code to the forum, i asked for it twice. You want the help, so the least you can do when i ask for it is to provide your code

The biggest problem is index shift, you need to understand this concept, i explained it here:

Practice Makes Perfect, anti_vowel, 8/15

once you realize this is a problem, there are two things you can do.

  1. Not removing from the list you are looping over, so you will need to copy of the list (so you have one list to remove from and one to loop over)
  2. looping and removing in reverse (from right to left), so you don’t have a problem with index shift

then you need to give a deep thought about how to check if value is already in the list, and when to remove.

furthermore, you need to realize what different methods and functions there are to remove items from a list, and which best fits your solution. For example, .remove() wouldn’t be suitable for approach 2.


def remove_duplicates(x):

for i in range(0, len(x)):
    for j in range(i + 1, len(x)):
        if x[j] == x[i]:

return x

if i work through it on paper, it seems to work fine

out of topic question: according to you how much time a below average student needs to complete this whole course i.e. the one i am doing now


you should also swap your logic, you need to check the values which are already in the list, not the values to come (so condition second for loop, that is easier)


ok thank you, i will try everything you suggested and come up with something better than this


If list.remove doesn’t do what you want then you had not decided on an approach yet

Whatever you really meant should happen, that is the approach, and it might not involve list.remove at all (it probably didn’t, because it would be silly and you wouldn’t come up with it)

Instead, decide what should happen. And then use code to describe exactly that. That’s what code is for, describing what you have decided should happen.
That’s why everyone’s protesting, you needed to describe what you meant should happen instead of saying that it involves list.remove

You might also ignore what list.remove actually does, and only rely on what its result is regardless of how it gets there. In that case you’d need to describe all the rest of the things that you need to carry out. You would already know what those actions are, or you’d be able to reason it out easily. Then you could describe them and make a convincing argument for why you’re not able to write it without a bit of help with something.

Forget about python while you’re deciding what should happen. Make sure you’ve decided before you write anything. And stick to what you decided (unless you prove it wrong, in which case, rethink what you meant)

Whatever you do manually to complete the task is probably a good model for what to do. You might want to scale it up to a larger list to think about how much work it would be, but you might also only go for a correct solution that is only reasonable for small inputs, that’s fine too.

If what you did with pen and paper was to cross out duplicates, then keep in mind that in your program you probably simply wouldn’t re-visit it and therefore have no reason to remove it. Crossing out is also not what list.remove does, the equivalent would instead be to overwrite it with some value for “crossed out”, preferably a unique value that can’t ever be in the input (one way to accomplish that is to create an object and test whether it’s the same value (not equal. same one.))


I noticed that, whilst we were quite vocal in trying to convince you to complete this exercise in a different way, we never really responded to your questions about why it didn’t work.

I would still encourage you to revise your approach, although @stetim94 and I have been exchanging ideas on how we’d get this to work doing it your way (i.e. removing items from the list). Attempting that has been thoroughly enjoyable.

Anyway, enough of the “you’re doing it the hard way” stuff. There’s some value to be had in reviewing your error from a learning perspective, so let’s do it!

Here’s your code, with the console output at the bottom.

def remove_duplicates(x):
  #y = []
  for i in range(0, len(x)):
    for j in range(i+1, len(x)):
      if x[j] == x[i]:
        #print x
  #y = x
  return x

print remove_duplicates([1,1,1,1,2,2,2,3,3,3,3,5,5,6,7,7])

[1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 6, 7, 7]
[1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 6, 7, 7]
Traceback (most recent call last):
  File "python", line 10, in <module>
  File "python", line 5, in remove_duplicates
IndexError: list index out of range

You’re getting the IndexError because your for loop evaluates your range() statement to create the sequence it iterates over, but it doesn’t re-evaluate the sequence if anything changes. So, to begin with, your range range(0, len(x)) evaluates to range(0, 16) and range(i+1, len(x)) evaluates to range(1, 16).

Once we’re into your for loops, they start comparing values as so. (The values being compared are in bold):

[1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 6, 7, 7]
[1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 6, 7, 7]

They match, so it removes a 1. Then, it compares the following:

[1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 6, 7, 7]
[1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 6, 7, 7]

They match, so it removes another 1, and goes on to compare the following:

[1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 6, 7, 7]
[1, 1, 2, 2, 2, 3, 3, 3, 3, 5, 5, 6, 7, 7]

1 does not equal 2, obviously, so Python will continue along the shortened list to check if anything else is a 1. The problem is that your list is now 14 items long, so the last 7 is in index 13, but because you told Python to check x[j] for every index from 1 to 16 it will eventually try and retrieve the item in x[14].

There is no item at that index, so Python gives you the error you see in the console.

This index shifting problem is what the exercise is trying to steer you away from by telling you not to modify the original list that you pass to the function.

Hope that helps a bit. :slight_smile:


thank you @thepitycoder , i actually did find out this solution when i studied the flow chart of for loop(this is the first time i experienced a dynamic length so i skipped that part, for loop never changes the condition) and i realized my mistake , now all i have to do is just figure out a way to do that and it already feels overwhelming. Anyway, i solved this exercise using different method but i would still try my luck with .remove() . thank you for being so supportive, no body actually told me the problem with my code so eventually i stopped asking . if you dont mind can you please help me with “classes and objects in python”, i am having a hard time understanding this concept, is there any additional study material i can refer to in addition to this course material ?


As I mentioned before, stetim94 and I were exchanging ideas on how we’d do it via message on the forum. We both went down different routes, but I think each was equally frustrating in terms of getting it to work. Got there in the end, but if we were doing this for any reason other than “is this actually doable?” we’d have canned the idea waaaaaaaaaay before solving it I think.

We found a few ways of doing it, although only one method specifically used .remove(). The others used a different process to remove the items from the list. It can definitely be done, but it’s not the most elegant solution…

Yeah, sorry - I think we just got a bit caught up in trying to steer you down a more efficient route and didn’t actually think to stop and explain why the attempt you made in the code in the OP didn’t work as you thought. Sorry about that. :frowning:

Do you have another topic for that? If not, I’d suggest making one, otherwise if anyone finds this thread in the future (having the same difficulty with .remove() that you were) they might get confused. :slight_smile: I’ll take a look in a minute and see if I can find your classes/objects topic, in case you have already made it. :slight_smile:


Here’s the link to the new topic :slight_smile: Additional content for python classes and objects


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