Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 removing duplicate numbers from a list
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

for num in uniquify(numbers):
    # do_something
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Removing Certain Numbers From File chascp 2 172 Feb-07-2020, 04:04 PM
Last Post: chascp
  Removing duplicate Image Evil_Patrick 8 442 Jan-26-2020, 03:48 AM
Last Post: Evil_Patrick
  output a list of random numbers 'x' columns wide adityavpratap 4 112 Jan-13-2020, 05:32 PM
Last Post: perfringo
  How do I name a list while generating numbers on the fly? Pleiades 6 239 Dec-03-2019, 05:47 PM
Last Post: jefsummers
  Removing duplicate list items eglaud 4 206 Nov-22-2019, 08:07 PM
Last Post: ichabod801
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 5 395 Nov-21-2019, 09:02 PM
Last Post: Pleiades
  Subtract 11 from entire list of quoted numbers Pleiades 1 155 Nov-14-2019, 10:26 AM
Last Post: Larz60+
  removing items from a list or group within a for loop. allusernametaken 9 409 Nov-13-2019, 01:58 AM
Last Post: allusernametaken
  List of numbers to add Planeuge 4 308 Sep-21-2019, 10:41 PM
Last Post: Planeuge
  can itertools compact a list removing all of some value? Skaperen 6 441 Sep-02-2019, 03:19 AM
Last Post: Skaperen

Forum Jump:

Users browsing this thread: 1 Guest(s)