# remove_duplicates error: list index out of range (code posted)

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

remove_duplicates

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

I get this error after running the code: list index out of range

<What do you expect to happen instead?>

I reached the end of the loops so I thought it’d be okay. I read that the list might be damaged or faulty due to the process I used. I really didn’t read the hints until after I was done so I wanted to see if I could get by with what I made instead of just redoing it the simple way.

I added a lot of print statements to double check the logic and it seemed okay to me. Is there a way to make this run properly or should I just give up on it and follow what the hint said? I read something about list comprehension but I couldn’t quite grasp that concept yet.

Any help is appreciated. Learning Python is fun but I’m not very good at it yet.

```python

words_x = [4, 5, 5, 4]

def remove_duplicates(words_x):
really_new_list =
new_list =
new_list.append(words_x[0])
print “Setting initial value for new_list!”, new_list
for x in xrange(0, len(words_x)):
match = 0
print “Resetting match variable!”, match
new_range = len(new_list)
print “New range is”, len(new_list)
for y in xrange(0, new_range):
if lower(str(words_x)) == lower(str(new_list[y])):
print “Duplicate detected!”, words_x, new_list[y]
match += 1
print match
print “Y is”, y
else:
print “Clear!”,
print words_x, new_list[y]
print “Y is”, y

``````    if match == 0:
new_list.append(words_x[x])
print new_list

print "List finalized! (Code line: 26)", new_list

return new_list
``````

remove_duplicates(words_x)

``<do not remove the three backticks above>``

#2

Is this a exercise? I can’t even get the code to run, i added a function call:

``````words_x = [4, 5, 5, 4]

def remove_duplicates(words_x):
really_new_list = []
new_list = []
new_list.append(words_x[0])
print "Setting initial value for new_list!", new_list
for x in xrange(0, len(words_x)):
match = 0
print "Resetting match variable!", match
new_range = len(new_list)
print "New range is", len(new_list)
for y in xrange(0, new_range):
if lower(str(words_x[x])) == lower(str(new_list[y])):
print "Duplicate detected!", words_x[x], new_list[y]
match += 1
print match
print "Y is", y
else:
print "Clear!",
print words_x[x], new_list[y]
print "Y is", y

if match == 0:
new_list.append(words_x[x])
remove_duplicates(words_x)
``````

Which results in a error:
Traceback (most recent call last):
File “python”, line 26, in
File “python”, line 14, in remove_duplicates
NameError: global name ‘lower’ is not defined

shouldn’t it be:

``````str(words_x[x]).lower()
``````

As you can see in the docs

#3

Oh, that’s odd. It ran in the exercise window earlier, but now when you mentioned it I tried again (I kept the window open) all this time and I got the same error. My apologies! I’ve been running it without that specific error earlier today but I’m very sure I didn’t change that line near the end. I was more or less editing the print statements to see where it was going. Unfortunately I don’t have a screenshot of it running from earlier.

I added your corrections and it ran again just now. Here’s the updated code. I’m sorry for the trouble. And yes, it’s an exercise.

``````words_x = [4, 5, 5, 4]

def remove_duplicates(words_x):
really_new_list = []
new_list = []
new_list.append(words_x[0])
print "Setting initial value for new_list!", new_list
for x in xrange(0, len(words_x)):
match = 0
print "Resetting match variable!", match
new_range = len(new_list)
print "New range is", len(new_list)
for y in xrange(0, new_range):
if str(words_x[x]).lower() == str(new_list[y]).lower():
print "Duplicate detected!", words_x[x], new_list[y]
match += 1
print match
print "Y is", y
else:
print "Clear!",
print words_x[x], new_list[y]
print "Y is", y

if match == 0:
new_list.append(words_x[x])
print new_list

print "List finalized! (Code line: 26)", new_list

return new_list

remove_duplicates(words_x)

``````

#4

Wow, this must be the most lines of code i have ever seen for this problem. Don’t you think you are making it yourself rather complicated? The exercise is mean, it also throws you an empty list:

``````words_x = []
``````

your program seems unable to handle it. Sorry, that might be a bit rude. But 33 lines of code is a lot to solve this problem

#5

It’s okay. It actually runs and throws out the list with the desired result except for that error at the end. I read it was due to manipulating lists while using them in loops. I will dig out the solution somewhere else for this and I will just use the standard solution to bypass the problem with what was listed in the hint section.

Thank you for your time. :3

#6

When looking for duplicates, treat words with upper case as different from words with lower case, in other words, let your code retain its native case sensitivity. Don’t change case.

Work with a single loop to iterate from one end of the data to the other. Your data should not be mutated. The result should be an extraction from that data to form a list of uniques.

``````def find_uniques(x):
words = x.split(' ')
uniques = []
for word in words:
if word not in uniques:
uniques.append(word)
return uniques

print find_uniques("The quick brown fox jumps over the lazy dog")
# ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
``````

Notice that case-sensitity is respected in the above?

Edit:

Just realized that the name I used on my example function is misleading. It is not finding
uniques, only returning a list of uniques. Finding uniques would mean
returning a list of items that only occur once in the original data.
That will be a little trickier, but we may as well take a crack at it, just for practice.

#7

3 posts were split to a new topic: Word and number frequency, finding uniques