Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python program to print Rows where all its Elements' frequency is greater than K
Next article icon

Python Program to Extract Rows of a matrix with Even frequency Elements

Last Updated : 04 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a Matrix, the task is to write a Python program to extract all the rows which have even frequencies of elements.

Examples:

Input: [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Output: [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]
Explanation:  frequency of 4-> 4 which is even frequency of 2-> 2 which is even frequency of 6-> 2 which is even frequency of 5-> 2 which is even

Method 1 : Using list comprehension, Counter() and all()

In this, count is maintained using Counter(), and all() is used to check if all frequencies are even, if not, row is not entered in result list.

Program:

Python3
from collections import Counter  # initializing list test_list = [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2],              [6, 5, 6, 5], [1, 2, 3, 4]]  # printing original list print("The original list is : " + str(test_list))  # Counter() gets the required frequency res = [sub for sub in test_list if all(     val % 2 == 0 for key, val in list(dict(Counter(sub)).items()))]  # printing result print("Filtered Matrix ? : " + str(res)) 

Output
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]

Time Complexity: O(M*N)
Auxiliary Space: O(K)

Method 2: Using filter(), Counter() and items()

Similar to the above method, the difference being filter() and lambda function is used for the task of filtering even frequency rows.

Program:

Python3
from collections import Counter  # initializing list test_list = [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2],              [6, 5, 6, 5], [1, 2, 3, 4]]  # Printing original list print("The original list is : " + str(test_list))  # Counter() gets the required frequency # filter() used to perform filtering res = list(filter(lambda sub: all(val % 2 == 0 for key,                                   val in list(dict(Counter(sub)).items())), test_list))  # Printing the result print("Filtered Matrix ? : " + str(res)) 

Output
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]] 

Time Complexity: O(m*n), where m and n is the number of rows and columns in the list “test_list”.
Auxiliary Space: O(k), where k is the number of elements 

Method 3 : Using set() and count() methods

Python3
# Python Program to Extract Rows of a matrix with Even frequency Elements # initializing list test_list = [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2],              [6, 5, 6, 5], [1, 2, 3, 4]]  # printing original list print("The original list is : " + str(test_list)) res = [] for i in test_list:     x = set(i)     c = 0     for j in x:         if(i.count(j) % 2 == 0):             c += 1     if(c == len(x)):         res.append(i) # printing result print("Filtered Matrix ? : " + str(res)) 

Output
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]

Method 4 : Using set() and operator.countOf() methods

Python3
# Python Program to Extract Rows of a matrix with Even frequency Elements # initializing list import operator as op test_list = [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2],              [6, 5, 6, 5], [1, 2, 3, 4]]  # printing original list print("The original list is : " + str(test_list)) res = [] for i in test_list:     x = set(i)     c = 0     for j in x:         if(op.countOf(i,j) % 2 == 0):             c += 1     if(c == len(x)):         res.append(i) # printing result print("Filtered Matrix ? : " + str(res)) 

Output
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]

Time Complexity: O(n*n )
Auxiliary Space:  O(n*n)

Method 5: Using nested for loop +all()

Python3
test_list = [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] print("The original list is : " + str(test_list)) result = [] for sub in test_list:     count = dict()     for ele in sub:         if ele % 2 == 0:             count[ele] = count.get(ele, 0) + 1     if all(val % 2 == 0 for val in count.values()):         result.append(sub) print("Filtered Matrix ? : " + str(result)) #This code is contributed by Jyothi pinjala. 

Output
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]

Time Complexity: O(n*n)
Auxiliary Space:  O(n)

Method 6: Using only built-in Python functions

Python3
test_list = [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2],              [6, 5, 6, 5], [1, 2, 3, 4]]  # printing original list print("The original list is : " + str(test_list))  res = [] for row in test_list:     counts = {}     for elem in row:         counts[elem] = counts.get(elem, 0) + 1     if all(count % 2 == 0 for count in counts.values()):         res.append(row)  # printing result print("Filtered Matrix ? : " + str(res)) 

Output
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]

Time complexity: O(n^2) where n is the size of the input list of lists. 
Auxiliary space: O(n) to store the resulting list of lists.


Next Article
Python program to print Rows where all its Elements' frequency is greater than K
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python program to print Rows where all its Elements' frequency is greater than K
    Given Matrix, extract all rows whose all elements have a frequency greater than K. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2 Output : [[1, 1, 1, 1]] Explanation : Here, frequency of 1 is 4 > 2, hence row retained. Input : test_list = [[1, 1, 2, 3
    8 min read
  • Python program to remove rows with duplicate element in Matrix
    Given Matrix, remove all rows which have duplicate elements in them. Input : test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [[4, 3, 2]] Explanation : [4, 3, 2] is the only unique row. Input : test_list = [[4, 3, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [] Explanation : No
    5 min read
  • Python - Extract elements with equal frequency as value
    Given a list, extract all the elements having same frequency as its value. Examples: Input : test_list = [4, 3, 2, 2, 3, 4, 1, 3, 2, 4, 4] Output : [1, 3, 4] Explanation : All elements occur equal times as their value.  Input : test_list = [4, 3, 2, 2, 3, 4, 1, 3, 2, 4] Output : [1, 3] Explanation :
    6 min read
  • Python program to extract rows with common difference elements
    Given a Matrix, extract rows with AP sequence. Input : test_list = [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]] Output : [[4, 7, 10], [8, 10, 12], [6, 8, 10]] Explanation : 3, 4, and 2 are common difference in AP. Input : test_list = [[4, 7, 10], [8, 10, 13], [10, 11, 13], [6, 8, 10]] Output
    3 min read
  • Python Program to sort rows of a matrix by custom element count
    Given Matrix, the following program shows how to sort rows of a matrix by the count of presence of numbers from a specified list. Input : test_list = [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]], cus_list = [4, 5, 7] Output : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]] Explanation : 0 < 1 = 1 < 3 i
    5 min read
  • Python program to extract rows from Matrix that has distinct data types
    Given a Matrix, the task is to write a Python program to extract rows with no repeated data types. Examples: Input : test_list = [[4, 3, 1], ["gfg", 3, {4:2}], [3, 1, "jkl"], [9, (2, 3)]]Output : [['gfg', 3, {4: 2}], [9, (2, 3)]]Explanation : [4, 3, 1] are all integers hence omitted. [9, (2, 3)] has
    6 min read
  • Python Program that prints the rows of a given length from a matrix
    Given a Matrix, the following articles shows how to extract all the rows with a specified length. Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3 Output : [[1, 4, 6], [7, 3, 1]] Explanation : Extracted lists have length of 3.Input : test_list = [[3, 4, 5, 6], [1
    4 min read
  • Python Program for Frequencies of even and odd numbers in a matrix
    Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 1
    4 min read
  • Python Program that filters out non-empty rows of a matrix
    Given Matrix, the following article shows how to filter all the Non-Empty rows of a matrix. In simpler terms, the codes provided below return a matrix after removing empty rows from it. Input : test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []] Output : [[4, 5, 6, 7], [9, 8, 1]] Explanation : All emp
    4 min read
  • Python Program to Remove First Diagonal Elements from a Square Matrix
    Given a square matrix of N*N dimension, the task is to write a Python program to remove the first diagonal. Examples: Input : test_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Output : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4,
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences