Search code examples
pythonlistword-frequency

Frequency of elements in a list of list


My goal is to count frequency of words in a list of list. So, I have:

list_1 = [['x', 'y', 'z'], ['x', 'y', 'w'], ['w', 'x', 'y']]

My goal is something like:

x:3, y:3, w:2, z:1

Solution

  • You can use Counter:

    >>> from collections import Counter
    >>> Counter(elem for sub in list_1 for elem in sub)
    Counter({'x': 3, 'y': 3, 'w': 2, 'z': 1})