Python Forum

Full Version: removing duplicate numbers from a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
But creating a copy of the list makes the runtime even worse.
In addition to what i said above not only numbers.count()
has to iterate over the whole list, so has numbers.remove()
as this method has to search for the element to be removed.
If n == len(numbers) worst runtime is O(n) + O(n)*2*O(n)
If n is big you really don“t want this to be.
If lists are huge (thus memory can be a problem) and one doesn't need list but stream on unique values in order of appearance then generator can be option:

def uniquify(lst):
    seen = set()
    for item in lst:
        if item not in seen:
            yield item
            seen.add(item)

for num in uniquify(numbers):
    # do_something
In this case the set grows and grows if the entropy is high.
At some point you need a database, which returns unique values.
But in the most cases beginners don't do this kind of tasks.
Pages: 1 2