Sort Elements by Frequency

Program to sort elements by frequency.

n = int(input())
li = list(map(int,input().split()))[:n]
li.sort()
d = {}
for ele in li:
  d[ele] = li.count(ele)
li = []
while len(d)!=0:
  key = None
  value = None
  for k,v in d.items():
    if value==None or value<v:
      value = v
      key = k
  li.extend([key]*value)
  d.pop(key)
print(*li)

Input
10
2 3 2 5 6 2 3 9 5 6
Output
2 2 2 3 3 5 5 6 6 9