15. Remove Dupes : Shot down twice by the interpreter


Please look as these two code attempts and let me know where my brainfart is.

EXERCISE 14. Remove Duplicates

The first one gives:remove_duplicates([4, 5, 5, 4]) resulted in an error: list index out of range

def remove_duplicates(x):
    myset = set()
    for i in x:
        if i not in myset:
    return myset


The second one: SyntaxError: invalid syntax

def remove_duplicates(x):
    myset = set()
    return [myset.add(x[i]) if i not in myset for i in x]


i is an item, so needn't be expressed as x[i], just i.


The question is raised about the use of set(). By definition a set has no duplicates so we could just as easily return a set from the inputs, no algorithm required.

However, for practice, an algo is the way to learn at this stage, so I would recommend using a list, and .append() uniques to that list.

u = []
for i in x:
    if i not in u:
return u

When we correct the syntax error(s),

def remove_duplicates(x):
    myset = set()
    return [myset.add(i) for i in x if i not in myset]

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

[None, None, None, None, None, None]

As we can see, the return value of myset.add(i) is None, as can be said of any function with no return value.


regarding the first attempt.

what actually stopped it from running? what caused the list index out of range error? is a set not indexable(?) ?


The original list contains integers. The error was caused when the program attempted to access index[4] of x when there is none. The list only goes up to index[3].

We get around that issue by treating i as a list item, not an index.

For clarity, concerning set,

def remove_duplicates(x):
    return set(x)

That's it.


>>> set([1,2,3,2,3,4,5,4,5,6])
{1, 2, 3, 4, 5, 6}


def remove_duplicates(x):
    myset = set()
    for i in x:
        if i not in myset:
    return myset

when we got to i, and the value of i was 4 or 5, I was asking for
position 4 of x when x contained only positions 0-3
this was instead of just adding the value of i to myset.

is my understanding clear?


Not really. See the post just above. We do not need an algo to extract a set of uniques from a list. A set is by definition made up of uniques.


i understand that part. I was talking about the index failure.


In the above, i is a list item.

u = []
for i in range(len(x)):
    if x[i] not in u:
return u

In the above i is an index of x. We need to draw the distinction (and abandon the set() idea).


you've corrected my understanding of the use of a set.

I am trying now to see if I understand why the error of index out of range occured so that I know what to look for next time. I reused the original code only to keep the index failure example intact.

u = []
for i in range(len(x)):
    if i not in u:
return u

so as I read it, using your corrections, but with my index/append error still in place, what is happening is u.append(x[i]) was attempting to append 'something' to u from x at position i. the 'something' should have been just the value of i, instead of a position within x.

very confusing trying to describe what I was confused about :slight_smile: I hope my meaning made it across.


I get your meaning and it looks like you are on to something. Just remember,

i in x              => i is a list item, the actual look-up value

i in range(len(x))  => i is the index (position number) of the value

In the former, i is treated as the data type of the value. The latter has only one type, integer.



hey, can you pass up a complaint?

it's really friggin hard to troubleshoot problems when the system ignores print commands you put in to track values. I've been having to go to repl.it and using their console to troubleshoot.

they have console/editor for both 2.7 and 3.5


It is what it is. 2.7.x on this site. Drawing the comparisons is on the user since it is not taken up in the older course tracks. It does come up a bit in the forums, but mostly in passing. We need to let the learner learn. Python 2.x is a long way off being put on the shelf. It is still reasonable to accept it as quasi de facto.

Pursuant to learning any language we begin with the foundation and build upon that. Evolution and generations takes care of the rest. Of course we are going to be interested in the newest version. In the case of Python, if you can do it in Python 3 you can do it in Python 2.

Methods that in 2.x returned lists now return objects in 3.x.

print range(10,101,10)

Play with that in both versions.

Yes to exploring and ramping up on environment. Not so sure this module is where to 'bring it on', though it might well be, in 2.x.

Truth is I've been sneaking in 3.x code that still runs as expected in 2.x, but only in examples, not solutions.


don't get me wrong, that site, repl.it, teaches you nothing. I like CA. I was just trying to pass up the chain something I think could be improved.

repl.it is just an editor with a console. but it's not suppressing print statement output like here. THAT is the only problem I have here. print statements are really useful in figuring out what you are doing wrong.

and you can choose which environment to work in so it can match the 2.7 of here.

it does have something useful though. you can save your code. so I have been saving copies of my working versions in case I try to answer someones question on CA.

I've also been using repl.it to try out code folks have been having problems with so I can see if I can find the fault. those print statements are handy for that. and I like learning from other's mistakes.


The point I am making is that we need to delve into people's errors and issues in the 2.x frame of reference. Let's not cross into unsupported territory. These courses are quite spcific in many instances and don't leave a lot of wiggle room. In other words, not the place to explore. Just do the exercise and proceed to the next.

We don't need an REPL to do it. Just keep re-doing the exercise. Print statements will echo in the console, whether the lesson passes or not. There is no penalty if the SCT squawks. Ignore it. The console is what to watch.


I think you have my WHOLE complaint confused. I will state it again.

it is hard to troubleshoot a problem when trying to code for an exercise when the print statements do not work.

now, what i have resorted to doing has nothing to do with the complaint itself. I was saying that since print statements are not working here, I went to the 2.7 console and editor at repl.it to try the code there. I copy my code from here and paste it there. print statements work there so I can figure out what I am doing wrong.

I am not sure how we got from what I am saying here to the replies you have given me. I do hope I have not caused an offense in voicing what I see as a problem.


To troubleshoot on this site we just do the lesson again. The REPL is running in the background. The submit button is our Enter key.

You are talking about a command line, direct exposure of the interactive mode. I get that, but still don't think we need it in any course modules. Really.

That is for the user to explore. We can run with a command line in the labs.


I'm just talking about the learner being able to use a print statement in the their code to see what value is in a variable.

I am stuck on one right now. I am getting an error, but since I cannot see what the value is, I cannot tell what calculation among several is giving a wrong value.


Give us a dump of your code to review and test. I am obviously on a goose chase. Need to regroup. New code oftern helps.


A post was split to a new topic: In this case the prints do work


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