Open In App

Python - Convert Frequency dictionary to list

Last Updated : 26 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Python
# input frequency dictionary
a = {'gfg' : 3, 'ide' : 2}

# list comprehension to create a list from the dictionary
b = [key for key in a for _ in range(a[key])]

# printing the result
print(b)

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.

Python
# input frequency dictionary
a = {'gfg' : 3, 'ide' : 2}

# empty list to store the result
b = []

# loop through the dictionary and extend the list with repeated keys
for key in a:
    b.extend([key] * a[key])

# printing the result
print(b)

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.

Python
# input frequency dictionary
a = {'gfg' : 3, 'ide' : 2}

# empty list to store the result
b = []

# loop through the dictionary and append repeated keys
for key in a:
    for _ in range(a[key]):
        b.append(key)

# printing the result
print(b)

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.

Python
import itertools

# input frequency dictionary
a = {'gfg' : 3, 'ide' : 2}

# using itertools.chain() to generate the list
b = list(itertools.chain.from_iterable([key] * a[key] for key in a))

# printing the result
print(b)

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

Next Article

Similar Reads