Sometimes, while working with Python dictionaries, we can have a problem in which we need to construct the list out of the values of dictionary. This task is reverse of finding frequency and has application in day-day programming and web development domain. Let’s discuss certain ways in which this task can be performed.
Input : test_dict = {‘gfg’ : 3, ‘ide’ : 2}
Output : [‘gfg’, ‘gfg’, ‘gfg’, ‘ide’, ‘ide’]Input : test_dict = {‘practice’ : 1, ‘write’ : 2, ‘ide’ : 4}
Output : [‘practice’, ‘write’, ‘write’, ‘ide’, ‘ide’, ‘ide’, ‘ide’]
Method #1: Using loop
This is brute way to solve this problem. In this, we iterate for dictionary and extract the frequency and replicate the elements at that frequency.
Python3
# Python3 code to demonstrate working of # Convert Frequency dictionary to list # Using loop # initializing dictionary test_dict = { 'gfg' : 4 , 'is' : 2 , 'best' : 5 } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Convert Frequency dictionary to list # Using loop res = [] for key in test_dict: for idx in range (test_dict[key]): res.append(key) # printing result print ( "The resultant list : " + str (res)) |
The original dictionary : {'gfg': 4, 'is': 2, 'best': 5} The resultant list : ['gfg', 'gfg', 'gfg', 'gfg', 'is', 'is', 'best', 'best', 'best', 'best', 'best']
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2: Using list comprehension
This method is similar to above method with respect to working. This is shorthand to above method.
Python3
def construct_list(dictionary): # Use list comprehension to repeat the keys of the dictionary # value number of times and return the result as a list return [key for key, value in dictionary.items() for _ in range (value)] # initializing dictionary test_dict = { 'gfg' : 4 , 'is' : 2 , 'best' : 5 } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # printing result print ( "The resultant list : " + str (construct_list(test_dict))) |
The original dictionary : {'gfg': 4, 'is': 2, 'best': 5} The resultant list : ['gfg', 'gfg', 'gfg', 'gfg', 'is', 'is', 'best', 'best', 'best', 'best', 'best']
Method 3: Using the built-in itertools.repeat() and itertools.chain.from_iterable()
Step-by-step approach
- Import the itertools module. This module provides functions for creating iterators for efficient looping.
- Define a function construct_list that takes a dictionary as input.
- Create a list iterators using a list comprehension that iterates through the items in the dictionary. For each key-value pair in the dictionary, create an iterator using the itertools.repeat() function. The iterator repeats the key value times.
- Concatenate all the iterators in the iterators list using the itertools.chain.from_iterable() function. This function takes an iterable of iterables as input and returns a single iterable that concatenates all the input iterables.
- Convert the concatenated iterable into a list using the list() constructor and return the result.
- Define a dictionary test_dict and initialize it with key-value pairs.
- Print the original dictionary using the print() function and the str() constructor to convert the dictionary to a string.
- Call the construct_list() function with test_dict as input and print the resultant list using the print() function and the str() constructor to convert the list to a string.
Python3
import itertools def construct_list(dictionary): # Create a list of iterators that produce the keys of the dictionary iterators = [itertools.repeat(key, value) for key, value in dictionary.items()] # Concatenate all the iterators and return the result as a list return list (itertools.chain.from_iterable(iterators)) # initializing dictionary test_dict = { 'gfg' : 4 , 'is' : 2 , 'best' : 5 } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # printing result print ( "The resultant list : " + str (construct_list(test_dict))) |
The original dictionary : {'gfg': 4, 'is': 2, 'best': 5} The resultant list : ['gfg', 'gfg', 'gfg', 'gfg', 'is', 'is', 'best', 'best', 'best', 'best', 'best']
Time complexity: O(n), where n is the total number of values in the dictionary.
Auxiliary space: O(n), where n is the total number of values in the dictionary.