Count strings in nested list
A simpler and more performant approach would be to flatten the lists using itertools.chain
, and to count the strings with collections.Counter
:
from collections import Counter
from itertools import chain
Counter(chain.from_iterable(sentences))
Counter({'my': 3,
'first': 1,
'question': 1,
'in': 1,
'stackoverflow': 1,
'is': 2,
'favorite': 2,
'language': 1,
'python': 1})