# Exercise 15, Remove Duplicates, how can i improve this code, at the moment i loose the last entry of the list

#1

Hey everybody, i need help :

``````def remove_duplicates(replica_list):
new_replica_list=[]
no_replica_list=[]
for items_in_list in range(len(replica_list)):
new_replica_list.append(replica_list[items_in_list])
new_replica_list.sort()
for items_in_list in range(0,len(new_replica_list)-1):
if new_replica_list[items_in_list] !=new_replica_list[items_in_list+1]:
no_replica_list.append(new_replica_list[items_in_list])
return no_replica_list``````

I am loosing the last list entry, and i tried many different methods but non of them work efficiently, i need help please, or should one do this exercise completely different

#2

run this code:

``````def remove_duplicates(replica_list):
new_replica_list=[]
no_replica_list=[]
for items_in_list in range(len(replica_list)):
new_replica_list.append(replica_list[items_in_list])
new_replica_list.sort()
for items_in_list in range(0,len(new_replica_list)-1):
print new_replica_list[items_in_list], new_replica_list[items_in_list + 1]
if new_replica_list[items_in_list] !=new_replica_list[items_in_list+1]:
no_replica_list.append(new_replica_list[items_in_list])
return no_replica_list

print remove_duplicates([4,5,5,4])``````

its yours, with an additional print statement. The problem is that for the first 5, the if condition evaluates to false, so nothing is appended, and the second 5 is never checked (given this will give index error)

after the loop we could just cheat and check if the last value in no_replica_list doesn't equal the last value of new_replica_list, if so, append last value of new_replica_list to no_replica_list

#3

First of all, thanks alot for the tip,
i figured that one out, but now i have another problem:

``````def remove_duplicates(replica_list):
new_replica_list=[]
no_replica_list=[]
for items_in_list in range(len(replica_list)):# copy old to new
new_replica_list.append(replica_list[items_in_list])
new_replica_list.sort()
for items_in_list in range(0,len(new_replica_list)-1):
print new_replica_list[items_in_list], new_replica_list[items_in_list + 1]
if new_replica_list[items_in_list] !=new_replica_list[items_in_list+1]:
no_replica_list.append(new_replica_list[items_in_list])
if new_replica_list[0] == new_replica_list[-1]: # check for the fact if everything is the same
no_replica_list.append(new_replica_list[0])
if no_replica_list[-1] != new_replica_list[-1]:# include last list entry
no_replica_list.append(new_replica_list[-1])
return no_replica_list``````

i get an error message:

Oops, try again. remove_duplicates([]) resulted in an error: list index out of range
i dont even know now which index is meant ...

but when checked with:
print remove_duplicates([6,6,6,6])
my returned list is [6]

#4

you should check with;

``print remove_duplicates([])``

when the list supplied as argument is empty, your code will give an index error

we could just keep hacking, at the beginning of the function check if list is empty, if so, return empty list

But it indicates your solution is not ideal, perfectly speaking you would need to go back to the drawing board and redesign your function.

#5

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