Python – Cumulative Row Frequencies in a List

When you need to count how many times specific elements appear in each row of a nested list, you can use Python's Counter class from the collections module combined with list comprehension to calculate cumulative row frequencies.

What are Cumulative Row Frequencies?

Cumulative row frequencies refer to counting the total occurrences of specified elements within each row of a nested list. For example, if you want to find how many times elements [19, 2, 71] appear in each row, you sum their individual frequencies per row.

Example

Here's how to calculate cumulative row frequencies using Counter and list comprehension ?

from collections import Counter

my_list = [[11, 2, 32, 4, 31], [52, 52, 3, 71, 71, 3], [1, 3], [19, 19, 40, 40, 40]]

print("The list is:")
print(my_list)

my_element_list = [19, 2, 71]

my_frequency = [Counter(element) for element in my_list]

my_result = [sum([freq[word] for word in my_element_list if word in freq]) for freq in my_frequency]

print("The resultant matrix is:")
print(my_result)
The list is:
[[11, 2, 32, 4, 31], [52, 52, 3, 71, 71, 3], [1, 3], [19, 19, 40, 40, 40]]
The resultant matrix is:
[1, 2, 0, 2]

How It Works

The solution works in two main steps:

  • Step 1: Create Counter objects for each row using [Counter(element) for element in my_list]

  • Step 2: For each Counter, sum the frequencies of target elements using sum([freq[word] for word in my_element_list if word in freq])

Breaking Down the Results

Let's examine what happens for each row:

from collections import Counter

my_list = [[11, 2, 32, 4, 31], [52, 52, 3, 71, 71, 3], [1, 3], [19, 19, 40, 40, 40]]
my_element_list = [19, 2, 71]

for i, row in enumerate(my_list):
    counter = Counter(row)
    print(f"Row {i}: {row}")
    print(f"Counter: {counter}")
    
    total = sum([counter[word] for word in my_element_list if word in counter])
    print(f"Cumulative frequency of {my_element_list}: {total}")
    print()
Row 0: [11, 2, 32, 4, 31]
Counter: Counter({11: 1, 2: 1, 32: 1, 4: 1, 31: 1})
Cumulative frequency of [19, 2, 71]: 1

Row 1: [52, 52, 3, 71, 71, 3]
Counter: Counter({52: 2, 3: 2, 71: 2})
Cumulative frequency of [19, 2, 71]: 2

Row 2: [1, 3]
Counter: Counter({1: 1, 3: 1})
Cumulative frequency of [19, 2, 71]: 0

Row 3: [19, 19, 40, 40, 40]
Counter: Counter({40: 3, 19: 2})
Cumulative frequency of [19, 2, 71]: 2

Alternative Approach

You can also write this more explicitly without nested list comprehension ?

from collections import Counter

my_list = [[11, 2, 32, 4, 31], [52, 52, 3, 71, 71, 3], [1, 3], [19, 19, 40, 40, 40]]
target_elements = [19, 2, 71]

result = []
for row in my_list:
    counter = Counter(row)
    row_frequency = 0
    
    for element in target_elements:
        if element in counter:
            row_frequency += counter[element]
    
    result.append(row_frequency)

print("Result:", result)
Result: [1, 2, 0, 2]

Conclusion

Using Counter with list comprehension provides an efficient way to calculate cumulative row frequencies. The Counter class handles frequency counting automatically, while list comprehension enables concise iteration through nested structures.

Updated on: 2026-03-26T01:47:30+05:30

367 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements