0

I'm new to python. Could someone help me understand why the following function doesn't work? It is supposed to return a new list with the duplicates removed but instead prints [4,6].

def remove_duplicates(l):
    solution = []
    for item in l:
        if l.count(item) < 2:
            solution.append(item)
        else:
            l.remove(item)
    return solution


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

I thought it iterates one item at a time in a list. So the first 5 would have a count of 3 and be remove, the second five would have a count of 2 and be removed and the third 5 would have a count of 1 and be appended to the solution list. I can't wrap my head around why the 5s would be completely removed but the 4s would not.

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
  • 2
    Possible duplicate of [How do you remove duplicates from a list whilst preserving order?](https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order) – Arkistarvh Kltzuonstev Oct 05 '19 at 19:55
  • This [graphic](https://stackoverflow.com/questions/6500888/removing-from-a-list-while-iterating-over-it) might help you better understand whats going on. – Darjusch Oct 05 '19 at 20:04

2 Answers2

0

Use set data type in python to remove the duplicates.

a = [4,5,5,5,4,6]
solution = list(set(a))

Output:

[4,5,6]
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
Rajeshkumar
  • 59
  • 1
  • 1
  • 9
0

You must not remove items from a list, you are iterating at the moment. Iterating is done by incrementing an index internally.

If you want to keep the last occurence of an item, best, count them at first:

from collections import Counter
def remove_duplicates(l):
    solution = []
    counts = Counter(l)
    for item in l:
        if counts[item] == 1:
            solution.append(item)
        else:
            counts[item] -= 1
    return solution
Daniel
  • 42,087
  • 4
  • 55
  • 81