I wrote the following python function to remove duplicates from a given list:
def remove_dup(mylist):
for item in mylist:
while mylist.count(item) > 1:
mylist.remove(item)
return mylist
It seems to work with lists of integer but as soon as I include a string it does not perform as expected. For ex:
ml=[1, 1, 'b', 1, 1, 'a', 45, 2, 2, 'b', 2, 2, 3, 'a', 3, 3, 45]
remove_dup(ml)
['b', 1, 'b', 2, 'a', 3, 45]
So I get 'b' twice in the list. I am new to Python, but why is it?