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

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:

- Python | Find position of a character in given string
- Python | Find sum of frequency of given elements in the list
- Python | Find minimum of each index in list of lists
- Python | Find common elements in list of lists
- Python | Frequency of each character in String
- Python | Program to count number of lists in a list of lists
- Python | Sorting list of lists with similar list elements
- Python | Merge List with common elements in a List of Lists
- Python | List frequency of elements
- Python | Maximum sum of elements of list in a list of lists
- Python | Ways to sum list of lists and return sum list
- Python | Check if a list exists in given list of lists
- Python | Element with largest frequency in list
- Python | Finding frequency in list of tuples
- Python | Sort list elements by frequency

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.