Reputation: 1308
I have a dictionary
d = {1: 3, 5: 6, 10: 2}
I want to convert it to a list that holds the keys of the dictionary. Each key should be repeated as many times as its associated value.
I've written this code that does the job:
d = {1: 3, 5: 6, 10: 2}
l = []
for i in d:
for j in range(d[i]):
l.append(i)
l.sort()
print(l)
Output:
[1, 1, 1, 5, 5, 5, 5, 5, 5, 10, 10]
But I would like it to be a list comprehension. How can this be done?
Upvotes: 3
Views: 690
Reputation: 13727
Counter.elements()
method does exactly this:
from collections import Counter
d = {1: 3, 5: 6, 10: 2}
c = Counter(d)
result = list(c.elements())
print(result)
# [1, 1, 1, 5, 5, 5, 5, 5, 5, 10, 10]
Upvotes: 1
Reputation: 23206
One approach is to use itertools.chain
to glue sublists together
>>> list(itertools.chain(*[[k]*v for k, v in d.items()]))
[1, 1, 1, 10, 10, 5, 5, 5, 5, 5, 5]
Or if you are dealing with a very large dictionary, then you could avoid constructing the sub lists with itertools.chain.from_iterable
and itertools.repeat
>>> list(itertools.chain.from_iterable(itertools.repeat(k, v) for k, v in d.items()))
[1, 1, 1, 10, 10, 5, 5, 5, 5, 5, 5]
Comparative timings for a very large dictionary with using a list comprehension that uses two loops:
>>> d = {i: i for i in range(100)}
>>> %timeit list(itertools.chain.from_iterable(itertools.repeat(k, v) for k, v in d.items()))
10000 loops, best of 3: 55.6 µs per loop
>>> %timeit [k for k, v in d.items() for _ in range(v)]
10000 loops, best of 3: 119 µs per loop
It's not clear whether you want your output sorted (your example code does not sort it), but if so simply presort d.items()
# same as previous examples, but we sort d.items()
list(itertools.chain(*[[k]*v for k, v in sorted(d.items())]))
Upvotes: 1
Reputation: 1268
You can do it using a list comprehension:
[i for i in d for j in range(d[i])]
yields:
[1, 1, 1, 10, 10, 5, 5, 5, 5, 5, 5]
You can sort it again to get the list you were looking for.
Upvotes: 2
Reputation: 114038
[k for k,v in d.items() for _ in range(v)]
... I guess...
if you want it sorted you can do
[k for k,v in sorted(d.items()) for _ in range(v)]
Upvotes: 1