I have a list
list1 = [4,6,2,2,2,6,4,4,4]
I want to sort it based on the frequency of its elements.
After sorting the list should look like:
[4,4,4,4,2,2,2,6,6]
I have stored the element and element frequency in a dictionary. I want to sort this dictionary acc. to its value and not key. This is the part i am not able to code.
I have written the following code. I have the frequency of the elements but i dont know how to sort it.
def checkio(data):
list1 = [4,6,2,2,2,6,4,4,4]
list2 = list(dict.fromkeys(list1))
print(list2)
d = {}
list4, result = [], []
for i in list2:
d[i] = list1.count(i)
for i in list2:
list3 = [i]*d[i]
list4.append(list3)
for sublist in list4:
for item in sublist:
result.append(item)
return(result)