Given a list of lists, the task is to find the frequency of a character at every position of sub-list in list of lists.

Input :lst = [['X', 'Y', 'X'], ['Z', 'Y', 'X'], ['Y', 'Y', 'Y'], ['Z', 'Z', 'X'], ['Y', 'Z', 'X']], character = 'X'Output:[0.2, 0.0, 0.8]

**Explanation:**

We have 3 elements in each sublist, we have to find position of ‘X’ at position 0, 1 and 2. For Position **0** in all sublist we have –

‘x’ in first sub list at zero position,

‘z’ in second sub list at zero position,

‘y’ in third sub list at zero position,

‘z’ in fourth sub list at zero position and

‘y’ in fifth sub list at zero position.

So, we have 1 occurrence of ‘x’ at position 1 in all sub list so, Occurrence = 1/5 = .2

For Position **1** we don’t have any occurrence of ‘x’ in sub list so, Occurrence = 0/5 = 0.

For Position **2 ** we have 4 occurrence of ‘x’ in sub list so, Occurrence = 4/5 = 0.8

Let’s discuss certain ways in which this can be performed.

**Method #1 : Using Iteration**

`# Python code to find frequency of a character ` `# at every position of list in list of lists. ` ` ` `# Input list initialization ` `Input` `=` `[[` `'X'` `, ` `'Y'` `, ` `'X'` `], [` `'Z'` `, ` `'Y'` `, ` `'X'` `], ` ` ` `[` `'Y'` `, ` `'Y'` `, ` `'Y'` `], [` `'Z'` `, ` `'Z'` `, ` `'X'` `], ` ` ` `[` `'Y'` `, ` `'Z'` `, ` `'X'` `]] ` `Output ` `=` `[] ` ` ` `# Character Initialization ` `character ` `=` `'X'` ` ` `# Output list initialization ` `for` `elem ` `in` `range` `(` `len` `(` `Input` `[` `0` `])): ` ` ` `Output.append(` `0` `) ` ` ` `# Using iteration ` `for` `elem ` `in` `Input` `: ` ` ` `for` `x, y ` `in` `enumerate` `(elem): ` ` ` `if` `y ` `=` `=` `character: ` ` ` `Output[x]` `+` `=` `1` `for` `x, y ` `in` `enumerate` `(Output): ` ` ` `Output[x] ` `=` `y ` `/` `len` `(` `Input` `) ` ` ` `# Printing ` `print` `(` `"Initial list of list is :"` `, ` `Input` `) ` `print` `(` `"Occurrence of 'X' in list is"` `, Output) ` |

*chevron_right*

*filter_none*

**Output:**

Initial list of list is : [[‘X’, ‘Y’, ‘X’], [‘Z’, ‘Y’, ‘X’], [‘Y’, ‘Y’, ‘Y’], [‘Z’, ‘Z’, ‘X’], [‘Y’, ‘Z’, ‘X’]]

Occurrence of ‘X’ in list is [0.2, 0.0, 0.8]

**Method #2 : Using zip**

`# Python code to find frequency of a character ` `# at every position of list in list of lists. ` ` ` `# Input list initialization ` `Input` `=` `[[` `'X'` `, ` `'Y'` `, ` `'X'` `], [` `'Z'` `, ` `'Y'` `, ` `'X'` `], ` ` ` `[` `'Y'` `, ` `'Y'` `, ` `'Y'` `], [` `'Z'` `, ` `'Z'` `, ` `'X'` `], ` ` ` `[` `'Y'` `, ` `'Z'` `, ` `'X'` `]] ` ` ` `Output ` `=` `[] ` ` ` `# Character initialization ` `character ` `=` `'X'` ` ` `# Using zip ` `Output ` `=` `[elem.count(character)` `/` `len` `(elem) ` ` ` `for` `elem ` `in` `zip` `(` `*` `Input` `)] ` ` ` `# Printing ` `print` `(` `"Initial list of list is :"` `, ` `Input` `) ` `print` `(` `"Occurrence of 'X' in list is"` `, Output) ` |

*chevron_right*

*filter_none*

**Output:**

Initial list of list is : [[‘X’, ‘Y’, ‘X’], [‘Z’, ‘Y’, ‘X’], [‘Y’, ‘Y’, ‘Y’], [‘Z’, ‘Z’, ‘X’], [‘Y’, ‘Z’, ‘X’]]

Occurrence of ‘X’ in list is [0.2, 0.0, 0.8]

**Method #3: Using Pandas**

`# Python code to find frequency of a character ` `# at every position of list in list of lists. ` ` ` `import` `pandas as pd ` ` ` `# Input list initialization ` `Input` `=` `[[` `'X'` `, ` `'Y'` `, ` `'X'` `], ` ` ` `[` `'Z'` `, ` `'Y'` `, ` `'X'` `], ` ` ` `[` `'Y'` `, ` `'Y'` `, ` `'Y'` `], ` ` ` `[` `'Z'` `, ` `'Z'` `, ` `'X'` `], ` ` ` `[` `'Y'` `, ` `'Z'` `, ` `'X'` `]] ` ` ` `# Defining character ` `character ` `=` `'X'` ` ` `# using pandas ` `Output ` `=` `pd.DataFrame(` `Input` `) ` `Output ` `=` `Output.where(Output ` `=` `=` `character, ` `0` `).where(Output !` `=` `character, ` `1` `) ` ` ` `# Printing ` `print` `(` `"Initial list of list is :"` `, ` `Input` `) ` `print` `(` `"Occurrence of 'X' in list is\n"` `, Output.mean()) ` |

*chevron_right*

*filter_none*

**Output:**

Initial list of list is : [[‘X’, ‘Y’, ‘X’], [‘Z’, ‘Y’, ‘X’], [‘Y’, ‘Y’, ‘Y’], [‘Z’, ‘Z’, ‘X’], [‘Y’, ‘Z’, ‘X’]]

Occurrence of ‘X’ in list is

0 0.2

1 0.0

2 0.8

dtype: float64

## Recommended Posts:

- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Python program to find the character position of Kth word from a list of strings
- Python | Insert character after every character pair
- Python | Program to count number of lists in a list of lists
- Python | Selective Merge list every Nth position
- Python | Find position of a character in given string
- Python - Sort String list by K character frequency
- Python - Elements frequency count in multiple lists
- Python - Values frequency across Dictionaries lists
- Python | Check if a list exists in given list of lists
- Python | Zipping two lists of lists
- Python - Convert Lists into Similar key value lists
- Generate a list using given frequency list
- Python | Find sum of frequency of given elements in the list
- Python - All Position Character Combination
- Python | Frequency of each character in String
- Python | Construct string from character frequency tuple
- Python | Maximum frequency character in String
- Python - Expand Character Frequency String
- Python program to concatenate every elements across lists

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.