# Python – Dictionary List Values Frequency

Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the task of computing frequency of all the values in dictionary values lists. This is quite common problem and can have use cases in many domains. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {1: [‘gfg’, ‘CS’, ‘cool’], 2: [‘gfg’, ‘CS’]}
Output : {‘gfg’: 2, ‘CS’: 2, ‘cool’: 1}

Input : test_dict = {1 : [‘gfg’, ‘CS’]}
Output : {‘gfg’: 1, ‘CS’: 1}

Method #1 : Using defaultdict() + loop
The combination of above functions can be used to solve this problem. In this, we use defaultdict() to initialize the counter for each value and brute force is used to increment the counter in dictionary lists.

## Python3

 `# Python3 code to demonstrate working of ` `# Dictionary List Values Frequency` `# Using loop + defaultdict()` `from` `collections ``import` `defaultdict`   `# initializing dictionary` `test_dict ``=` `{``1` `: [``'gfg'``, ``'best'``, ``'geeks'``], ` `             ``2` `: [``'gfg'``, ``'CS'``], ` `             ``3` `: [``'best'``, ``'for'``, ``'CS'``], ` `             ``4` `: [``'test'``, ``'ide'``, ``'success'``],` `             ``5` `: [``'gfg'``, ``'is'``, ``'best'``]}`   `# printing original dictionary` `print``(``"The original dictionary : "` `+` `str``(test_dict))`   `# Dictionary List Values Frequency` `# Using loop + defaultdict()` `res ``=` `defaultdict(``int``)` `for` `key, val ``in` `test_dict.items():` `    ``for` `sub ``in` `val:` `        ``res[sub] ``+``=` `1` `    `  `# printing result ` `print``(``"Values Frequency : "` `+` `str``(``dict``(res))) `

Output :
The original dictionary : {1: [‘gfg’, ‘best’, ‘geeks’], 2: [‘gfg’, ‘CS’], 3: [‘best’, ‘for’, ‘CS’], 4: [‘test’, ‘ide’, ‘success’], 5: [‘gfg’, ‘is’, ‘best’]}
Values Frequency : {‘gfg’: 3, ‘best’: 3, ‘geeks’: 1, ‘CS’: 2, ‘for’: 1, ‘test’: 1, ‘ide’: 1, ‘success’: 1, ‘is’: 1}

Time Complexity: O(nm), where n is the total number of keys in the input “test_dict” and m is the total number of elements in all the lists in the dictionary values.

Space Complexity: O(k), where k is the total number of unique elements in all the lists in the dictionary values.

Method #2 : Using chain.from_iterables() + Counter()
The combination of above functions can be used to solve this problem. In this, we perform task of Counting using Counter() and the flattening/extraction of values is done using from_iterables().

## Python3

 `# Python3 code to demonstrate working of ` `# Dictionary List Values Frequency` `# Using chain.from_iterables() + Counter()` `from` `collections ``import` `Counter` `from` `itertools ``import` `chain`   `# initializing dictionary` `test_dict ``=` `{``1` `: [``'gfg'``, ``'best'``, ``'geeks'``], ` `             ``2` `: [``'gfg'``, ``'CS'``], ` `             ``3` `: [``'best'``, ``'for'``, ``'CS'``], ` `             ``4` `: [``'test'``, ``'ide'``, ``'success'``],` `             ``5` `: [``'gfg'``, ``'is'``, ``'best'``]}`   `# printing original dictionary` `print``(``"The original dictionary : "` `+` `str``(test_dict))`   `# Dictionary List Values Frequency` `# Using chain.from_iterables() + Counter()` `res ``=` `Counter(chain.from_iterable(test_dict.values()))` `    `  `# printing result ` `print``(``"Values Frequency : "` `+` `str``(``dict``(res))) `

Output :
The original dictionary : {1: [‘gfg’, ‘best’, ‘geeks’], 2: [‘gfg’, ‘CS’], 3: [‘best’, ‘for’, ‘CS’], 4: [‘test’, ‘ide’, ‘success’], 5: [‘gfg’, ‘is’, ‘best’]}
Values Frequency : {‘gfg’: 3, ‘best’: 3, ‘geeks’: 1, ‘CS’: 2, ‘for’: 1, ‘test’: 1, ‘ide’: 1, ‘success’: 1, ‘is’: 1}

Method #3: Using Counter() + list comprehension

• Import Counter from collections
• Define the original dictionary test_dict
• Use a list comprehension to flatten the values of test_dict into a list of all values
• Use Counter to count the frequency of each value in the flattened list
• Print the resulting dictionary of value frequencies

## Python3

 `from` `collections ``import` `Counter`   `test_dict ``=` `{``1` `: [``'gfg'``, ``'best'``, ``'geeks'``], ` `             ``2` `: [``'gfg'``, ``'CS'``], ` `             ``3` `: [``'best'``, ``'for'``, ``'CS'``], ` `             ``4` `: [``'test'``, ``'ide'``, ``'success'``],` `             ``5` `: [``'gfg'``, ``'is'``, ``'best'``]}`   `res ``=` `Counter([val ``for` `sublist ``in` `test_dict.values() ``for` `val ``in` `sublist])` `print``(``"Values Frequency : "` `+` `str``(``dict``(res)))`

Output

`Values Frequency : {'gfg': 3, 'best': 3, 'geeks': 1, 'CS': 2, 'for': 1, 'test': 1, 'ide': 1, 'success': 1, 'is': 1}`

Time complexity: O(n), where n is the total number of values in test_dict.
Auxiliary space: O(n), for the list of all values.

Method #4 : Using values(),list(),set(),extend(),count() methods

Approach

1. Store the values of dictionary test_dict in x(using list(),values() method) which is a nested list
2. Convert nested list to list y  using  extend and for loop
3. Store unique values of y in z using list(),set() methods, create an empty dictionary res
4. Initiate a for loop over list z and assign values of z as keys and the count of those values in y as values to dictionary res
5. Display res

## Python3

 `# Python3 code to demonstrate working of` `# Dictionary List Values Frequency`   `# initializing dictionary` `test_dict ``=` `{``1` `: [``'gfg'``, ``'best'``, ``'geeks'``],` `            ``2` `: [``'gfg'``, ``'CS'``],` `            ``3` `: [``'best'``, ``'for'``, ``'CS'``],` `            ``4` `: [``'test'``, ``'ide'``, ``'success'``],` `            ``5` `: [``'gfg'``, ``'is'``, ``'best'``]}`   `# printing original dictionary` `print``(``"The original dictionary : "` `+` `str``(test_dict))`   `# Dictionary List Values Frequency` `x``=``list``(test_dict.values())` `y``=``[]` `for` `i ``in` `x:` `    ``y.extend(i)` `z``=``list``(``set``(y))` `res``=``dict``()` `for` `i ``in` `z:` `    ``res[i]``=``y.count(i)` `# printing result` `print``(``"Values Frequency : "` `+` `str``(``dict``(res)))`

Output

```The original dictionary : {1: ['gfg', 'best', 'geeks'], 2: ['gfg', 'CS'], 3: ['best', 'for', 'CS'], 4: ['test', 'ide', 'success'], 5: ['gfg', 'is', 'best']}
Values Frequency : {'test': 1, 'gfg': 3, 'ide': 1, 'best': 3, 'CS': 2, 'is': 1, 'success': 1, 'for': 1, 'geeks': 1}```

Time Complexity : O(N) N -length of extended list(y)

Auxiliary Space : O(N) N – length of output dictionary

Method #5 : Using numpy:

Algorithm :

1. Create a dictionary test_dict.
2. Use numpy.hstack() to flatten all the values in the dictionary into a single list named list_values.
3. Use numpy.unique() to count the frequency of each item in the list list_values and return the unique values and their counts in two separate lists named unique and counts.
4. Use dict(zip()) to create a new dictionary frequency_dict where unique and counts are zipped together as key-value pairs.

5. Print the frequency dictionary

## Python3

 `import` `numpy as np`   `test_dict ``=` `{``1` `: [``'gfg'``, ``'best'``, ``'geeks'``],` `            ``2` `: [``'gfg'``, ``'CS'``],` `            ``3` `: [``'best'``, ``'for'``, ``'CS'``],` `            ``4` `: [``'test'``, ``'ide'``, ``'success'``],` `            ``5` `: [``'gfg'``, ``'is'``, ``'best'``]}` `# printing original dictionary` `print``(``"The original dictionary : "` `+` `str``(test_dict))` ` `  `# Using numpy.hstack() to flatten dictionary values into a single list` `list_values ``=` `np.hstack(``list``(test_dict.values()))`   `# Using numpy.unique() to count frequency of each item in the list` `unique, counts ``=` `np.unique(list_values, return_counts``=``True``)`   `# Creating the frequency dictionary` `frequency_dict ``=` `dict``(``zip``(unique, counts))`   `# printing result` `print``(``"Values Frequency : "` `+` `str``(frequency_dict))` `#This code is contrinuted by Pushpa.`

```Output:
The original dictionary : {1: ['gfg', 'best', 'geeks'], 2: ['gfg', 'CS'], 3: ['best', 'for', 'CS'], 4: ['test', 'ide', 'success'], 5: ['gfg', 'is', 'best']}
Values Frequency : {'CS': 2, 'best': 3, 'for': 1, 'geeks': 1, 'gfg': 3, 'ide': 1, 'is': 1, 'success': 1, 'test': 1}```

Time Complexity:

Flattening the dictionary values into a single list takes O(n) time, where n is the total number of values in all lists combined.
Counting the frequency of each item in the list takes O(k log k) time, where k is the number of unique items in the list.
Zipping the unique values and their counts as key-value pairs into a new dictionary takes O(k) time.
Therefore, the overall time complexity of the code is O(n + k log k).
Space Complexity:

The space used by the original dictionary test_dict is O(n).
The space used by the list_values list is also O(n).
The space used by the unique and counts lists is O(k).
The space used by the frequency_dict dictionary is also O(k).

Therefore, the overall space complexity of the code is O(n + k).

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next