How to sort a list with duplicate items by the biggest number of duplicate occurrences - Python
Question: I have a

    
    
    list1 = ["one", "two", "two", "three", "four" , "five", "five", "five", "six"]
    

and the Output should be

    
    
    list2 = ["five" , "two", "one", "three" , "six"]
    

  * `"five"` is the first element because in list1 has the highest number of occurrences (3)
  * `"two`" is the second element because in list1 has the next highest number of occurrences (2)
  * `"one"`, `"three`" and `"six"` have the same lower number of occurrences (1) so they are the last in my `list2` \- It doesn't really matter what position they will be as long as they are after "five" and "two". On short, `list2 = ["five" , "two", "six", "three" , "one"]` or `list2 = ["five" , "two", "three", "one" , "six"]` or any other variations are accepted.

I could solve this by creating a dictionary to store the number of occurances
and then create a new list with my items ordered by the dict

    
    
    my_dict = {i:list1.count(i) for i in list1}
    

but I need something cleaner


Answer: You could use a list comprehension and
[`Counter`](https://docs.python.org/3/library/collections.html#counter-
objects):

    
    
    from collections import Counter
    print([element for element,count in Counter(list1).most_common()])
    

Outputs:

    
    
    ['five', 'two', 'three', 'six', 'four', 'one']
    

