Python Forum
removing duplicate numbers from a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: removing duplicate numbers from a list (/thread-19134.html)

Pages: 1 2


RE: removing duplicate numbers from a list - ThomasL - Jun-15-2019

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.


RE: removing duplicate numbers from a list - perfringo - Jun-15-2019

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



RE: removing duplicate numbers from a list - DeaD_EyE - Jun-16-2019

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.