Python – Convert Frequency dictionary to list

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :



The original dictionary : {‘is’: 2, ‘best’: 5, ‘gfg’: 4}
The resultant list : [‘is’, ‘is’, ‘best’, ‘best’, ‘best’, ‘best’, ‘best’, ‘gfg’, ‘gfg’, ‘gfg’, ‘gfg’]

 

Method #2 : Using list comprehension
This method is similar to above method with respect to working. This is shorthand to above method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert Frequency dictionary to list
# Using list comprehension
  
# 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 list comprehension
res = [[key] * test_dict[key] for key in test_dict]
      
# printing result 
print("The resultant list : " + str(res)) 

chevron_right


Output :

The original dictionary : {‘is’: 2, ‘best’: 5, ‘gfg’: 4}
The resultant list : [‘is’, ‘is’, ‘best’, ‘best’, ‘best’, ‘best’, ‘best’, ‘gfg’, ‘gfg’, ‘gfg’, ‘gfg’]




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.