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

Life of Brian: Conjugate the verb, "to go" !
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
  List of numbers to add Planeuge 4 174 Sep-21-2019, 10:41 PM
Last Post: Planeuge
  can itertools compact a list removing all of some value? Skaperen 6 316 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  weird result trying to remove numbers from a list Exsul 6 197 Aug-27-2019, 05:10 AM
Last Post: perfringo
  How to count and order numbers in a list rachyioli 2 150 Aug-21-2019, 10:51 AM
Last Post: perfringo
  removing quotes from a list and keep type list evilcode1 3 190 Aug-03-2019, 11:07 AM
Last Post: perfringo
  huge list of whole numbers Skaperen 3 346 Jun-02-2019, 10:11 PM
Last Post: Skaperen
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 256 May-09-2019, 12:19 PM
Last Post: Pleiades
  how to add the numbers of a list Pedroski55 2 274 Apr-07-2019, 03:11 PM
Last Post: perfringo
  Sum of Numbers in a List MrGoat 8 556 Jan-27-2019, 06:03 PM
Last Post: aakashjha001
  How to add numbers in a list DragonG 3 527 Nov-17-2018, 10:27 PM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)