# Remove duplicates from list and order it by count? (python 3)

How do I remove all duplicates from list and order it based on count?

s = ["foo", "this", "lem", "foo", "lem"]
s = ["foo", "ebh", "wosv", "wosv", "ebh", "4whcu"]
#should return
>>> ["foo", "lem", "this"]
>>> ["ebh", "wosv", "foo", "4whcu"]

Rules:

• Must be shorter than the below code in terms of byte size.
• Order must be based on the count of in descending order.
• If there are two strings with the same count, the first appearance comes first
• All duplicates must be removed, only one of each string should be in the final list

I currently have

list({}.fromkeys(sorted(s,key=lambda y:-s.count(y))))

I've noticed answers like set but that arbitrarily changes the order, which I do not want.

Edit: Sorry for the poor quality before. I had whipped this right before I slept and I was quite tired. This is not for StackOverflow because I'm trying to golf/shorten the size of this code as much as possible. I've tried looking for answers but I haven't been able to find anything.

• Do you mean "shorter in terms of byte count"? Aug 19 '20 at 4:17
– xnor
Aug 19 '20 at 4:20
• This looks more like a stackoverflow question. Aug 19 '20 at 4:49
• I've edited the question a bit. Aug 19 '20 at 16:50
• Does the solution have to be a single expression? Or can we include multiple statements?
– xnor
Aug 20 '20 at 10:30

# 44 bytes

sorted({}.fromkeys(s),key=s.count,reverse=1)

Try it online!

An improvement to the below, replacing the key with a built-in rather than a lambda, and using reverse to swap the comparisons. We'd like to do [::-1] on the final result instead, but that doesn't do the right stable tiebreaks.

We could also use dict(zip(s,s)) in place of {}.fromkeys(s) for the same length.

# 47 bytes

sorted({}.fromkeys(s),key=lambda y:-s.count(y))

Try it online!

A simple transposition of your code, moving your {}.fromkeys de-duplication trick before the sorting. Since sorting converts to a list, extracting the keys from a dictionary, this saves the list() call of the original.

For ease of reference, the original code is:

53 bytes

list({}.fromkeys(sorted(s,key=lambda y:-s.count(y))))

Try it online!

• sorted({}.fromkeys(s),key=s.count,reverse=1) works as well at 44 bytes, since the reverse option doesn't just reverse the final output, but changes the actual sorting.
– ovs
Aug 20 '20 at 10:44
• @ovs Nice find, was just posting the same
– xnor
Aug 20 '20 at 10:45
• I'm curious though, why does s.count work in key? Aug 20 '20 at 20:16
• @12944qwerty Consider s.count and lambda x: s.count(x) - I think you will see that they are identical :) Aug 20 '20 at 22:01
• ohh, they both are functions and being called the same way.... Aug 20 '20 at 22:07

APL, 11 chars

∪⌷⍨∘⊂∘⍒⊢∘≢⌸

⊢∘≢⌸ right arg: number of apperance of each element, in order of first appearance

left arg: unique elements, in order of first appearance

⌷⍨∘⊂∘⍒ sorts elements of the left arg based on the grade-down of the right arg

• It’s not quite python.
– user7467
Nov 9 '21 at 21:48
• Ooops, sorry, I thought it was, like the other challenges, something to be done in the language of choice. Nov 10 '21 at 22:53