# Removing Duplicates - using double loops

#1

Hi everybody!
i thought double loops will work ok - but they don't.
The error message says "Oops, try again. remove_duplicates([4, 5, 5, 4]) returned [] instead of [4, 5]"
What's wrong with the code?
I know that i can use
" if i not in no_duplicates" function, but still would like to learn more about strange habits of double loops!

``````def remove_duplicates(old_list):
no_duplicates = []
for i in old_list:
for n in no_duplicates:
if i != n:
no_duplicates.append(i)
return no_duplicates``````

#2

i would add a print statement to see what is wrong:

``````def remove_duplicates(old_list):
no_duplicates = []
for i in old_list:
for n in no_duplicates:
print i, n
if i != n:
no_duplicates.append(i)
return no_duplicates
print remove_duplicates([4,5,5,4])``````

oh oops, the print statement is never reached. How come? the second loop, there is nothing in no_duplicates, so the loop never runs

i agree with @appylpye, double loops is not a good way to go, but i wanted to clarify why your double loop wasn't working

#3

Why do you need a nested loop? Use a single loop to iterate through `old_list`. For each `i` in the `list`, take a particular action if that `i` has already been encountered; take another action if it has not.

... and I agree with @stetim94, regarding using a `print` statement.

#4

Thanks for the nice tip with printing! And for the explanation too!

#5

Hi @appylpye , I've solved this problem by using for - else statement under for loop. Is there any better way to do it?

#6

please make a new topic with your code + question about improvement, this topic is for helping jenechnka, thank you

#8