Python – Values frequencies of key
Last Updated :
05 May, 2023
Sometimes, while working with Python dictionary lists, one can have a problem in which we require to find the frequency of all the values occurring in a specific key in dictionary list. This can have application across many domains including web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [{‘gfg’: [1]}, {‘good’: [5, 78, 10], ‘gfg’: [5, 6, 7, 8]}] Output : {8: 1, 1: 1, 5: 1, 6: 1, 7: 1} Input : test_list = [{‘gfg’: [1, 5, 6, 7]}] Output : {1: 1, 5: 1, 6: 1, 7: 1}
Method #1 : Using Counter() + list comprehension The combination of above functionalities is used to solve this problem. In this, we perform the task of finding frequency using Counter() and computation is done using list comprehension.
Python3
from collections import Counter
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ("The original list is : " + str (test_list))
freq_key = "gfg"
res = Counter([idx for val in test_list for idx in val[freq_key]])
print ("The frequency dictionary : " + str ( dict (res)))
|
Output :
The original list is : [{‘gfg’: [1, 5, 6, 7], ‘is’: [9, 6, 2, 10]}, {‘gfg’: [5, 6, 7, 8], ‘good’: [5, 7, 10]}, {‘gfg’: [7, 5], ‘best’: [5, 7]}] The frequency dictionary : {8: 1, 1: 1, 5: 3, 6: 2, 7: 3}
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using chain.from_iterables() + Counter() The combination of above methods can be used to solve this problem. In this, we perform the task of iteration and binding using chain.from_iterables().
Python3
from collections import Counter
import itertools
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ("The original list is : " + str (test_list))
freq_key = "gfg"
res = Counter(itertools.chain.from_iterable([sub[freq_key] for sub in test_list]))
print ("The frequency dictionary : " + str ( dict (res)))
|
Output :
The original list is : [{‘gfg’: [1, 5, 6, 7], ‘is’: [9, 6, 2, 10]}, {‘gfg’: [5, 6, 7, 8], ‘good’: [5, 7, 10]}, {‘gfg’: [7, 5], ‘best’: [5, 7]}] The frequency dictionary : {8: 1, 1: 1, 5: 3, 6: 2, 7: 3}
Time Complexity: O(n*n) where n is the number of elements in the dictionary. The chain.from_iterables() + Counter() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary.
Method #3 : Using extend(),list(),set(),count() methods
Approach
- Extracting all values of a particular key in list of dictionaries using for loop and extend() method
- Create a new list with all unique values of above values list
- Now create a dictionary with unique value as key and count of this value in the values list using count() method
- Display dictionary
Python3
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ( "The original list is : " + str (test_list))
freq_key = "gfg"
res = dict ()
x = []
for i in test_list:
x.extend(i[freq_key])
y = list ( set (x))
for i in y:
res[i] = x.count(i)
print ( "The frequency dictionary : " + str (res))
|
Output
The original list is : [{'gfg': [1, 5, 6, 7], 'is': [9, 6, 2, 10]}, {'gfg': [5, 6, 7, 8], 'good': [5, 7, 10]}, {'gfg': [7, 5], 'best': [5, 7]}]
The frequency dictionary : {1: 1, 5: 3, 6: 2, 7: 3, 8: 1}
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #4:Using Numpy
Algorithm
- Initialize the list of dictionaries.
- Print the original list.
- Initialize the frequency key for which we want to find the values frequency.
- Initialize an empty dictionary for storing the frequency of values of the frequency key.
- For each dictionary in the list, extend the values corresponding to the frequency key to a list x.
- Get the unique values of list x using set().
- For each unique value in the list of unique values, count the frequency of the value in the list x using count() function and store the frequency in the resultant dictionary.
- Print the frequency dictionary.
Python3
import numpy as np
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ( "The original list is:" , test_list)
freq_key = "gfg"
res = {}
flat_arr = np.concatenate([d[freq_key] for d in test_list])
unique, counts = np.unique(flat_arr, return_counts = True )
for i in range ( len (unique)):
res[unique[i]] = counts[i]
print ( "The frequency dictionary:" , res)
|
Output
The original list is: [{'gfg': [1, 5, 6, 7], 'is': [9, 6, 2, 10]}, {'gfg': [5, 6, 7, 8], 'good': [5, 7, 10]}, {'gfg': [7, 5], 'best': [5, 7]}]
The frequency dictionary: {1: 1, 5: 3, 6: 2, 7: 3, 8: 1}
The time complexity of the given code is O(N*M), where N is the number of dictionaries in the list and M is the maximum length of the list associated with the given frequency key.
The space complexity of the given code is also O(N*M), since we are creating a list to store all the values associated with the given frequency key, and then creating a dictionary with keys as unique values and their count as values.
Method 5 : Using a loop
step-by-step approach:
- Initialize an empty dictionary called “freq_dict” to store the frequency count of each value in the “gfg” key.
- Loop through each dictionary in the list “test_list”.
- Access the values of the “gfg” key in the current dictionary using the syntax “current_dict[‘gfg’]”.
- Loop through each value in the “gfg” key using a for loop.
- For each value in the “gfg” key, check if it is already a key in the “freq_dict” dictionary. If it is, increment its value by 1. If it is not, add it as a new key with value 1.
- After iterating through all the dictionaries in “test_list”, “freq_dict” will contain the frequency count of each value in the “gfg” key.
- Print the “freq_dict” dictionary.
Python3
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ( "The original list is:" , test_list)
freq_key = "gfg"
freq_dict = {}
for d in test_list:
for val in d[freq_key]:
if val in freq_dict:
freq_dict[val] + = 1
else :
freq_dict[val] = 1
print ( "The frequency dictionary:" , freq_dict)
|
Output
The original list is: [{'gfg': [1, 5, 6, 7], 'is': [9, 6, 2, 10]}, {'gfg': [5, 6, 7, 8], 'good': [5, 7, 10]}, {'gfg': [7, 5], 'best': [5, 7]}]
The frequency dictionary: {1: 1, 5: 3, 6: 2, 7: 3, 8: 1}
Time complexity: O(N*M), where N is the number of dictionaries in “test_list” and M is the average length of the “gfg” key in each dictionary.
Auxiliary space: O(K), where K is the number of unique values in the “gfg” key across all dictionaries in “test_list”.
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. Python is: A high-level language, used in web development, data science, automat
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow. Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples. The below Python section contains a wide collection of Python programming examples. These Python c
11 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
10 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list(). Let's look at a simple exa
3 min read
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Dictionaries in Python
A Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier t
5 min read