Python function which will count the total number of items in values from a dictionary and return another dictionary with item count

Question:

data = {'customer1': ['milk', 'bread'],
 'customer2': ['butter'],
 'customer3': ['beer', 'diapers'],
 'customer4': ['milk', 'bread', 'butter'],
 'customer5': ['bread']}

I want the Python function output to be

{'milk': 2, 'bread': 3, 'butter': 2, 'beer': 1, 'diapers': 1} 

and then also build a histogram on this data

res = dict()
for key in customer_data.keys():
  
    res[key] = len(set([sub[key] for sub in customer_data]))
Asked By: Shivangi Taneja

||

Answers:

You can use Counter class from collections module.

>>> data = {
...     "customer1": ["milk", "bread"],
...     "customer2": ["butter"],
...     "customer3": ["beer", "diapers"],
...     "customer4": ["milk", "bread", "butter"],
...     "customer5": ["bread"],
... }
>>> 
>>> from collections import Counter
>>>
>>> print(Counter([val for key, value in data.items() for val in value]))
Counter({'bread': 3, 'milk': 2, 'butter': 2, 'beer': 1, 'diapers': 1})

Alternativey you can also do,

>>> data = {
...     "customer1": ["milk", "bread"],
...     "customer2": ["butter"],
...     "customer3": ["beer", "diapers"],
...     "customer4": ["milk", "bread", "butter"],
...     "customer5": ["bread"],
... }
>>> 
>>> 
>>> 
>>> result = {}
>>> 
>>> for _, values in data.items():
...     for value in values:
...         result[value] = result.setdefault(value, 0) + 1
... 
>>> print(result)
{'milk': 2, 'bread': 3, 'butter': 2, 'beer': 1, 'diapers': 1}
Answered By: Abdul Niyas P M

I didn’t get the histogram part, but the counting part was some sort of a duplicated question (here)

from collections import Counter

count=Counter()
for val in data.values():
    count += Counter(val)

count = dict(count)

output: {'milk': 2, 'bread': 3, 'butter': 2, 'beer': 1, 'diapers': 1}
Answered By: Mehdi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.