Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python dictionary to duplicated list

How do you convert a dictionary to a duplicated list in python?

For example: {'a':1,'b':2,'c':1,'d':3} to ['a','b','b','c','d','d','d']

like image 299
本翰 張 Avatar asked Jan 14 '23 05:01

本翰 張


1 Answers

Counter.elements from the collections module does exactly that:

d = {'a':1,'b':2,'c':1,'d':3}
from collections import Counter
print sorted(Counter(d).elements())
# ['a', 'b', 'b', 'c', 'd', 'd', 'd']
like image 94
georg Avatar answered Jan 21 '23 13:01

georg