Skip to content Skip to sidebar Skip to footer

Convert Dictionary Into List With Length Based On Values

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

Solution 1:

You can do it using a list comprehension:

[i foriin d forjinrange(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.

Solution 2:

[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)]

Solution 3:

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 inrange(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 _ inrange(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())]))

Solution 4:

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]

Post a Comment for "Convert Dictionary Into List With Length Based On Values"