VOOZH about

URL: https://www.geeksforgeeks.org/python/python-convert-frequency-dictionary-to-list/

⇱ Python - Convert Frequency dictionary to list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Convert Frequency dictionary to list

Last Updated : 12 Jul, 2025

When we convert a frequency dictionary to a list, we are transforming the dictionary into a list of key-value or just the keys/values, depending on needs. We can convert a frequency dictionary to a list using methods such as list comprehension, loops, extend() method and itertools.chain() function.

Frequency Dictionary

A frequency dictionary is a data structure used to store the count of occurrences of each unique element in a collection, such as a list or string.

  • Key: The unique element from the collection (e.g., a character or number).
  • Value: The number of times that element appears in the collection.

Here's how we can convert a frequency dictionary to list in python:

Using List Comprehension

List comprehension is an efficient way to convert a frequency dictionary into a list. This method is preferred because it’s both readable and fast. In this approach, we will loop through each key in the dictionary and repeat that key based on its count (value).


Output
['gfg', 'gfg', 'gfg', 'ide', 'ide']

Other methods that we can use to convert a frequency dictionary to a list in Python are:

Using extend() Method

extend() method is another efficient way to add elements to a list. It works by adding multiple items to a list at once. We can use this method to build our final list by repeating the dictionary keys based on their counts. Instead of using append() to add each item one by one, extend() allows us to add multiple repeated items at once.


Output
['gfg', 'gfg', 'gfg', 'ide', 'ide']

Using for Loop

Using a basic for loop is the simplest way to convert a frequency dictionary to a list. In this approach, we loop through each key in the dictionary, repeat it according to its frequency, and add it to the list using the append() method.


Output
['gfg', 'gfg', 'gfg', 'ide', 'ide']

Using itertools.chain()

itertools.chain() function from the itertools module is a tool that allows us to combine multiple iterables (like lists or generators) into a single iterable. This can be useful when we want to handle repeated items from the dictionary efficiently. In this method, we will use itertools.chain() to combine repeated keys into one final list.


Output
['gfg', 'gfg', 'gfg', 'ide', 'ide']
  • The result is a single iterable that produces all the repeated keys in sequence, which we then convert into a list using list().
Comment