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']
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With