Python - Convert Frequency dictionary to list
Last Updated : 26 Dec, 2024
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']
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().
Similar Reads
Convert a Dictionary to a List in Python In Python, dictionaries and lists are important data structures. Dictionaries hold pairs of keys and values, while lists are groups of elements arranged in a specific order. Sometimes, you might want to change a dictionary into a list, and Python offers various ways to do this. How to Convert a Dict
3 min read
Convert Dictionary to String List in Python The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st
3 min read
Convert Nested Dictionary to List in Python In this article, weâll explore several methods to Convert Nested Dictionaries to a List in Python. List comprehension is the fastest and most concise way to convert a nested dictionary into a list.Pythona = { "a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}, } # Convert nested dictionary to a list of li
3 min read
Convert List of Lists to Dictionary - Python We are given list of lists we need to convert it to python . For example we are given a list of lists a = [["a", 1], ["b", 2], ["c", 3]] we need to convert the list in dictionary so that the output becomes {'a': 1, 'b': 2, 'c': 3}. Using Dictionary ComprehensionUsing dictionary comprehension, we ite
3 min read
Python Convert Dictionary to List of Values Python has different types of built-in data structures to manage your data. A list is a collection of ordered items, whereas a dictionary is a key-value pair data. Both of them are unique in their own way. In this article, the dictionary is converted into a list of values in Python using various con
3 min read
Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
3 min read