Open In App

Python Program to convert Dictionary to List by Repeating keys corresponding value times

Given a dictionary where keys are characters and their constituent values are numbers, the task here is to write a python program that can convert it to a list by repeating the key character value times.

Input : test_dict = {‘g’ : 2, ‘f’ : 3, ‘g’ : 1, ‘b’ : 4, ‘e’ : 1, ‘s’ : 4, ‘t’ : 3}
Output : [‘g’, ‘f’, ‘f’, ‘f’, ‘b’, ‘b’, ‘b’, ‘b’, ‘e’, ‘s’, ‘s’, ‘s’, ‘s’, ‘t’, ‘t’, ‘t’]
Explanation : f is added 3 times in list.



Input : test_dict = {‘g’ : 2, ‘f’ : 3, ‘g’ : 1, ‘b’ : 4, ‘e’ : 1}
Output : [‘g’, ‘f’, ‘f’, ‘f’, ‘b’, ‘b’, ‘b’, ‘b’, ‘e’]
Explanation : f is added 3 times in list.

Method 1 : Using loop and * operator



In this, elements are inserted using loop and occurrences are taken care using * operator.

Program:




# initializing dictionary
test_dict = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
res = []
for key, val in test_dict.items():
 
    # getting values using * operator
    res += [key] * val
 
# printing result
print("The constructed list : " + str(res))

Output
The original dictionary is : {'g': 1, 'f': 3, 'b': 4, 'e': 1, 's': 4, 't': 3}
The constructed list : ['g', 'f', 'f', 'f', 'b', 'b', 'b', 'b', 'e', 's', 's', 's', 's', 't', 't', 't']

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the total number of values in the dictionary.

Method 2 : Using list comprehension

This uses nested loop approach using list comprehension as one liner to solve this problem. Iterate the values as much as occurrence required.

Program:




# initializing dictionary
test_dict = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# nested list comprehension to solve problem
res = [key for key, val in test_dict.items() for _ in range(val)]
 
# printing result
print("The constructed list : " + str(res))

Output
The original dictionary is : {'g': 1, 'f': 3, 'b': 4, 'e': 1, 's': 4, 't': 3}
The constructed list : ['g', 'f', 'f', 'f', 'b', 'b', 'b', 'b', 'e', 's', 's', 's', 's', 't', 't', 't']

Time complexity: O(N), where N is the number of elements in the dictionary.
Auxiliary space: O(N), where N is the number of elements in the dictionary. 

Method 3: Using the collections module:




import collections
 
test_dict = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using collections.Counter to flatten the dictionary into a list
res = list(collections.Counter(test_dict).elements())
 
# printing result
print("The constructed list : " + str(res))

Output
The original dictionary is : {'g': 1, 'f': 3, 'b': 4, 'e': 1, 's': 4, 't': 3}
The constructed list : ['g', 'f', 'f', 'f', 'b', 'b', 'b', 'b', 'e', 's', 's', 's', 's', 't', 't', 't']

Time complexity: O(n), where n is the number of key-value pairs in the dictionary
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.

Method 4: Using a for loop and the dictionary’s values() method

This method uses a for loop to iterate over the dictionary’s values and extend the result list with the value repeated by its own value in the dictionary.




test_dict = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
res = []
 
for value in test_dict.values():
    res.extend([value] * value)
 
# printing result
print("The constructed list : " + str(res))

Output
The constructed list : [1, 3, 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 3, 3, 3]

Time Complexity: O(n), where n is the number of values in the dictionary. 
Auxiliary Space: O(n), as the new list created by the code can potentially be as large as the number of values in the dictionary.

Method 5: Using the itertools module:

Use itertools to repeat each key according to its value, and creates a new list with all the repeated keys. Finally, it prints the new list.




import itertools
 
# initializing dictionary
test_dict = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using itertools to repeat keys according to their values
res = list(itertools.chain.from_iterable(itertools.repeat(key, val) for key, val in test_dict.items()))
 
# printing result
print("The constructed list : " + str(res))

Output
The original dictionary is : {'g': 1, 'f': 3, 'b': 4, 'e': 1, 's': 4, 't': 3}
The constructed list : ['g', 'f', 'f', 'f', 'b', 'b', 'b', 'b', 'e', 's', 's', 's', 's', 't', 't', 't']

Time complexity: O(n), where n is the number of key-value pairs in the dictionary. 
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary. 

Method 6: Using the reduce function from functools module

This method uses the reduce() function from the functools module to iterate through the items of the dictionary, and then concatenates the list with the value repeated as many times as its value. The reduce() function reduces an iterable into a single value by applying a function to each element of the iterable. Here, the function used is a lambda function that takes a list lst and a tuple val representing a key-value pair of the dictionary, and appends val[1] (the value of the pair) repeated val[1] times to the list. The initial value of the list is []




from functools import reduce
 
test_dict = {'g': 2, 'f': 3, 'g': 1, 'b': 4, 'e': 1, 's': 4, 't': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
res = reduce(lambda lst, val: lst + [val[1]] * val[1], test_dict.items(), [])
 
# printing result
print("The constructed list : " + str(res))

Output
The original dictionary is : {'g': 1, 'f': 3, 'b': 4, 'e': 1, 's': 4, 't': 3}
The constructed list : [1, 3, 3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 3, 3, 3]

Time complexity: O(n), where n is the total number of items in the input dictionary. 
Auxiliary space: O(n), where n is the total number of items in the input dictionary.


Article Tags :