Python count items in dict value that is a list
Use sum()
and the lengths of each of the dictionary values:
count = sum(len(v) for v in d.itervalues())
If you are using Python 3, then just use d.values()
.
Quick demo with your input sample and one of mine:
>>> d = {'T1': ['eggs', 'bacon', 'sausage']}
>>> sum(len(v) for v in d.itervalues())
3
>>> d = {'T1': ['eggs', 'bacon', 'sausage'], 'T2': ['spam', 'ham', 'monty', 'python']}
>>> sum(len(v) for v in d.itervalues())
7
A Counter
won't help you much here, you are not creating a count per entry, you are calculating the total length of all your values.
>>> d = {'T1': ['eggs', 'bacon', 'sausage'], 'T2': ['spam', 'ham', 'monty', 'python']}
>>> sum(map(len, d.values()))
7