
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Count the frequency of matrix row length
When it is required to count the frequency of the matrix row length, it is iterated over and its frequency is added to the empty dictionary or incremented if found again.
Example
Below is a demonstration of the same
my_list = [[42, 24, 11], [67, 18], [20], [54, 10, 25], [45, 99]] print("The list is :") print(my_list) my_result = dict() for element in my_list: if len(element) not in my_result: my_result[len(element)] = 1 else: my_result[len(element)] += 1 print("The result is :") print(my_result)
Output
The list is : [[42, 24, 11], [67, 18], [20], [54, 10, 25], [45, 99]] The result is : {1: 1, 2: 2, 3: 2}
Explanation
A list is defined and is displayed on the console.
An empty dictionary is defined.
The list is iterated over, and if the specific length is not present in the dictionary, the length in the dictionary is assigned to 1.
Otherwise, it is incremented by 1.
This is the output that is displayed on the console.
- Related Articles
- Python – Sort Matrix by None frequency
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?
- Python program to count distinct words and count frequency of them
- Count Frequency of Highest Frequent Elements in Python
- Python – Count frequency of sublist in given list
- Python – Sort Matrix by Row Median
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j in C++
- Custom length Matrix in Python
- Python - Sort Matrix by Maximum Row element
- Program to find length of longest matrix path length in Python
- Count of elements of an array present in every row of NxM matrix in C++
- Find the maximum element of each row in a matrix using Python
- Python – Sort Matrix by Palindrome count
- Sort the matrix row-wise and column-wise using Python
- Python program to find the redundancy rates for each row of a matrix

Advertisements