Skip to content
Related Articles

Related Articles

Python – Convert Frequency dictionary to list

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 28 Apr, 2020

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

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.




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

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
Recommended Articles
Page :

Start Your Coding Journey Now!