Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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:

Python3




# 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:

Python3




# 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:

  • First, import the collections module.
  • Initialize the test_dict dictionary with key-value pairs.
  • Print the original dictionary using print(“The original dictionary is : ” + str(test_dict)).
  • Use the collections.Counter function to convert the dictionary into a Counter object, which is a subclass of dictionary. The Counter.elements() method returns an iterator over the elements of the Counter object, with each element repeated as many times as its count in the Counter.
  • Convert the iterator returned by Counter.elements() into a list using the list() function and assign it to the res variable.
  • Print the result using print(“The constructed list : ” + str(res)).

Python3




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.

Python3




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.

Python3




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

Python3




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.



Last Updated : 16 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads