Find frequency of given character at every position in list of lists in Python

Let's consider a scenario where you have a list which is made of lists as its elements. We are interested in finding the frequency of one character at different positions of the inner lists. Below example will clarify the requirement.

Consider a list of lists given below:

listA = [['a', 'a', 'b'],
         ['a', 'c', 'b'],
         ['c', 'a', 'b'],
         ['c', 'a', 'a']]
print("Original list of lists:")
print(listA)
Original list of lists:
[['a', 'a', 'b'], ['a', 'c', 'b'], ['c', 'a', 'b'], ['c', 'a', 'a']]

In the above list we have elements which are lists with 3 elements. If we consider the first inner list it has a,a,b at positions 0,1,2. Similarly for the third list it is c,a,b at positions 0,1,2. Considering all the inner lists we can say the frequency of 'a' at position 0 is 2, at position 1 is 3 and at position 2 is 1.

Using Pandas

The pandas library is extensively used for data manipulation by creating dataframes. We create a dataframe and use conditional logic to find if the value 'a' is present at each position ?

import pandas as pd

# Given list of lists
listA = [['a', 'a', 'b'],
         ['a', 'c', 'b'],
         ['c', 'a', 'b'],
         ['c', 'a', 'a']]

# Create DataFrame
df = pd.DataFrame(listA)
print("DataFrame:")
print(df)

# Count frequency of 'a' at each position
res = df.where(df == 'a', 0).where(df != 'a', 1)
frequency = res.sum()

print("\nOccurrence of 'a' at positions 0, 1 and 2:")
print(frequency)
DataFrame:
   0  1  2
0  a  a  b
1  a  c  b
2  c  a  b
3  c  a  a

Occurrence of 'a' at positions 0, 1 and 2:
0    2.0
1    3.0
2    1.0
dtype: float64

Using zip() Function

We can use zip() to transpose the list and then count occurrences at each position using list comprehension ?

# Given list of lists
listA = [['a', 'a', 'b'],
         ['a', 'c', 'b'],
         ['c', 'a', 'b'],
         ['c', 'a', 'a']]

# Transpose and count 'a' at each position
transposed = list(zip(*listA))
print("Transposed data:", transposed)

res = [elem.count('a') for elem in transposed]
print("\nOccurrence of 'a' at positions 0, 1 and 2:", res)
Transposed data: [('a', 'a', 'c', 'c'), ('a', 'c', 'a', 'a'), ('b', 'b', 'b', 'a')]

Occurrence of 'a' at positions 0, 1 and 2: [2, 3, 1]

Using Loops with enumerate()

A more explicit approach using nested loops to count character frequency at each position ?

# Given list of lists
listA = [['a', 'a', 'b'],
         ['a', 'c', 'b'],
         ['c', 'a', 'b'],
         ['c', 'a', 'a']]

target_char = 'a'
num_positions = len(listA[0])  # Assuming all sublists have same length

frequency = [0] * num_positions

for sublist in listA:
    for position, char in enumerate(sublist):
        if char == target_char:
            frequency[position] += 1

print(f"Occurrence of '{target_char}' at each position: {frequency}")
Occurrence of 'a' at each position: [2, 3, 1]

Comparison

Method Advantages Best For
Pandas Powerful for data analysis Complex data manipulation
zip() Concise and Pythonic Simple character counting
Loops Most readable and explicit Learning and debugging

Conclusion

Use zip() with list comprehension for the most Pythonic solution. Use pandas for complex data analysis scenarios. Use explicit loops when you need maximum clarity and control over the counting process.

Updated on: 2026-03-15T17:48:55+05:30

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements