Open In App

Dictionary to List by Repeating keys corresponding value times – Python

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a dictionary where each key is a character and its corresponding value is a number, the task is to generate a list by repeating each keys character according to its value and if a character is repeated as a key in the dictionary then the value from its last occurrence will be considered. For example, If we have a dictionary d = {‘g’: 2, ‘f’: 3, ‘g’: 1, ‘b’: 4, ‘e’: 1, ‘s’: 4, ‘t’: 3} then output will be [‘g’, ‘f’, ‘f’, ‘f’, ‘b’, ‘b’, ‘b’, ‘b’, ‘e’, ‘s’, ‘s’, ‘s’, ‘s’, ‘t’, ‘t’, ‘t’].

Using loop and * operator

In this method we use a loop to iterate through dictionary items and repeats the key value times using the * operator.

Python
d = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}

res = []
for k, v in d.items():
    res += [k] * v

print(res)

Output
['g', 'f', 'f', 'f', 'b', 'b', 'b', 'b', 'e', 's', 's', 's', 's', 't', 't', 't']

Using List Comprehension

This method uses list comprehension to repeat each dictionary key according to its value, creating a flat list of repeated elements.

Python
d = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}

# list comprehension repeat key `v` times
res = [k for k, v in d.items() for _ in range(v)]

print(res)

Output
['g', 'f', 'f', 'f', 'b', 'b', 'b', 'b', 'e', 's', 's', 's', 's', 't', 't', 't']

Explanation:

  • for k, v in d.items() retrieves the dictionary’s keys and their values.
  • for _ in range(v) ensures each key is repeated exactly v times in the output list.

Using the collections module

This method leverages collections.Counter to handle the dictionary and repeat the elements and return them as a list using list().

Python
import collections

d = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}

# using collections.Counter to flatten dictionary into a list
res = list(collections.Counter(d).elements())

print(res)

Output
['g', 'f', 'f', 'f', 'b', 'b', 'b', 'b', 'e', 's', 's', 's', 's', 't', 't', 't']

Explanation:

  • collections.Counter(d) creates a Counter object from the dictionary d.
  • .elements() returns an iterator that yields the elements repeated by their counts, which is converted to a list using list().

Using the itertools module

This method uses itertools.repeat to repeat keys based on their values and itertools.chain to flatten the results into a single list.

Python
import itertools

d = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}

# using itertools to repeat keys according to their values
res = list(itertools.chain.from_iterable(itertools.repeat(k, v) for k, v in d.items()))

print(res)

Output
['g', 'f', 'f', 'f', 'b', 'b', 'b', 'b', 'e', 's', 's', 's', 's', 't', 't', 't']

 Explanation:

  • itertools.repeat(k, v) repeats each key k v times.
  • itertools.chain.from_iterable() flattens the iterator of repeated keys into a single list.

Using reduce() from functools

In this method we use reduce() function to accumulate values by repeating the dictionary’s key values.

Python
from functools import reduce

d = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}

# using reduce to construct the list
res = reduce(lambda lst, val: lst + [val[0]] * val[1], d.items(), [])

print(res)

Output
['g', 'f', 'f', 'f', 'b', 'b', 'b', 'b', 'e', 's', 's', 's', 's', 't', 't', 't']

Explanation: reduce() function accumulates results by applying the lambda function, which repeats the key val[0] for val[1] times.



Next Article
Practice Tags :

Similar Reads