
- 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 Program to Extract Rows of a matrix with Even frequency Elements
When it is required to extract rows of a matrix with even frequency elements, a list comprehension with ‘all’ operator and ‘Counter’ method are used.
Example
Below is a demonstration of the same
from collections import Counter my_list = [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], [65, 57, 65, 57], [11, 24, 36, 48]] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all( value % 2 == 0 for key, value in list(dict(Counter(sub)).items()))] print("The result is :") print(my_result)
Output
The list is : [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], [65, 57, 65, 57], [11, 24, 36, 48]] The result is : [[41, 41, 41, 41, 22, 22], [65, 57, 65, 57]]
Explanation
A list of list is defined and is displayed on the console.
A list comprehension is used to iterate over the elements in the list, and the ‘all’ operator is used, to check if the value is divisibe by 2.
The elements of the list are accessed using ‘Counter’ and ‘dict’.
This is converted to a list and is assigned to a variable.
This is displayed as output on the console.
- Related Articles
- Python program to extract rows with common difference elements
- Python – Extract rows with Even length strings
- Python – Extract elements with equal frequency as value
- Python program to extract rows from Matrix that has distinct data types
- Python Program to Sort Matrix Rows by summation of consecutive difference of elements
- Python – Extract String elements from Mixed Matrix
- Python program to remove rows with duplicate element in Matrix
- Swift Program to calculate the sum of rows of matrix elements
- Golang Program to calculate the sum of rows of matrix elements
- Swift Program to Interchange Elements of First and Last Rows of a Matrix
- Find distinct elements common to all rows of a matrix in Python
- Python program to print Rows where all its Elements’ frequency is greater than K
- Python – Check Similar elements in Matrix rows
- Python program to extract Mono-digit elements
- Python – Extract rows with Complex data types

Advertisements