-4

So if there is a list: [1,1,2,3,4,5,5,5,6], the output would be [2,3,4,6] .

  • I've already gone through that thread, I've tried the code and all it does is remove duplicates, thats not what I'm trying to do, I'm trying to remove all numbers, where any one of them appear more than once – Otto Hausenberg Mar 08 '21 at 04:14
  • @rici your link seems to reduce everything to a set (lose information about multiplicity but not remove 1-count elements) – Razzle Shazl Mar 08 '21 at 04:14
  • @smac89 you link seems to reduce everything to a set, but OP needs are different. He does not want to drop duplicates and leave 1 behind, he wants to drop all that have duplicates. – Razzle Shazl Mar 08 '21 at 04:15
  • @razzle: fair enough – rici Mar 08 '21 at 04:16
  • 1
    Here's a better duplicate: https://stackoverflow.com/a/63905088/1566221 – rici Mar 08 '21 at 04:23
  • @rici that above link is for removing duplicates of lists of lists, based upon the first element in each list, that isn't related to the question I asked, I do appreciate the attempt, though razzle has understood what I mean and answered as such. I genuinely looked through about 100 different threads, but none pertain to the right set of criteria I mention here. – Otto Hausenberg Mar 08 '21 at 04:28
  • @otto: afaics, the only difference is that it uses `element[0]` instead of `element` to decide what a duplicate is. The solution is the same as razzle's: use a Counter. So it's not exactly the same question but it seems sufficiently close to me. Anyway, I retracted the CV. – rici Mar 08 '21 at 04:36
  • By the way, you can accept an answer without any rep. Just click on the checkmark.. – rici Mar 08 '21 at 04:38
  • @rici the format of them is different though, and since what I asked was only a small part of a larger program I'm writing, the style would be incompatible, thanks for letting me know that I could just select the check mark though – Otto Hausenberg Mar 08 '21 at 05:29

1 Answers1

0

Here is the function that you were looking for. The Iterable and the corresponding import is just to provide type hints and can be removed if you like.

from collections import Counter
from typing import Iterable
d = [1,1,2,3,4,5,5,5,6]

def retainSingles(it: Iterable):
    counts = Counter(it)
    return [c for c in counts if counts[c] == 1]

print(retainSingles(d))

Output:

[2, 3, 4, 6]
Razzle Shazl
  • 1,287
  • 1
  • 8
  • 20
  • Yes this is what I was looking for thanks, there are so many things on stack overflow about removing duplicates, but I couldn't find ones that actually worked, for where the count exceeded 1, so thanks so much – Otto Hausenberg Mar 08 '21 at 04:17
  • Hey @OttoHausenberg I'm glad that his solution worked for you. If you found this answer useful, please consider accepting. Thank you. – Razzle Shazl Mar 08 '21 at 04:21
  • Yes thanks for your help, I will do when I reach a level of 15, for that's apparently required to upvote on here. – Otto Hausenberg Mar 08 '21 at 04:29
  • @OttoHausenberg haha no worries points come easy after a while – Razzle Shazl Mar 08 '21 at 04:44