lst = [5, 6, 8, 9, 8, 8, 3, 4, 4, 6, 6]
counts, firstidx = count_and_first_index(lst)
sorted(lst, key=lambda x: (counts[x], -firstidx[x]), reverse=True)
python How do you sort list by occurrence with out removing elements from the list?