Q:

python How do you sort list by occurrence with out removing elements from the list?

Just wondering how do you sort a list by frequency/occurrence   in python 2.7 and if 2 elements occur the same number of times then the element that appears first in the original list is places ahead of the other element in the new list.
For example:
list = [5,6,8,9,8,8,3,4,4,6,6]
sorted_list = [6,6,6,8,8,8,4,4,5,9,3]

Any idea why the solution doesn't work for [1,3,3,3,2,2,2,1,1].The output is [3,3,3,2,2,2,1,1,1] but the correct output is [1,1,1,3,3,3,2,2,2]
Thanks again

A:

You can use the Counter class from collections as sorting key. Since you may have multiple elements with the same number of occurrences, you can use the value itself as a secondary sorting key so that identical elements are grouped together:
>>> from collections import Counter
>>> lst = [5,6,8,9,8,8,3,4,4,6,6] 
>>> c = Counter(lst)
>>> sorted(lst, key = lambda x : (c[x], x), reverse = True)
[8, 8, 8, 6, 6, 6, 4, 4, 9, 5, 3]

EDIT:
As MSeifert commented, ties should be broken by the order of first appearance, not the value of the element. This can be done using the index function on the original list:
>>> sorted(lst, key = lambda x : (-1 * c[x], lst.index(x)))
[6, 6, 6, 8, 8, 8, 4, 4, 5, 9, 3]

