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 that prints rows from the matrix that have same element at a given index
Next article icon

Python Program that prints the rows of a given length from a matrix

Last Updated : 28 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 4 
Output : [[3, 4, 5, 6]] 
Explanation : Extracted lists have length of 4. 

Method 1 : Using list comprehension and len()

In this, we perform the task of getting length using len() and list comprehension does the task of filtering all the rows which have a specified length.

Python3




# initializing list
test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# list comprehension is used for extracting K len rows
res = [sub for sub in test_list if len(sub) == K]
 
# printing result
print("The filtered rows : " + str(res))
 
 

Output:

The original list is : [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]

The filtered rows : [[1, 4, 6], [7, 3, 1]]

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

Method 2 : Using filter(), lambda and len()

In this, we perform the task of filtering using filter() and lambda. len() is used for finding length of rows.

Python3




# initializing list
test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# filter() + lambda to filter out rows of len K
res = list(filter(lambda sub: len(sub) == K, test_list))
 
# printing result
print("The filtered rows : " + str(res))
 
 

Output:

The original list is : [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]

The filtered rows : [[1, 4, 6], [7, 3, 1]]

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

Method 3: Using a for loop

Python3




# initializing list
test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using for loop to filter out rows of len K
res = []
for sub in test_list:
    if len(sub) == K:
        res.append(sub)
 
# printing result
print("The filtered rows : " + str(res))
#This code is contributed by Vinay Pinjala.
 
 
Output
The original list is : [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]] The filtered rows : [[1, 4, 6], [7, 3, 1]]

Time Complexity: O(n)

Auxiliary Space: O(n)

Method 4: Using the map() function

we can use the map() function along with a lambda function to create a new list containing only the sublists that have length K.

Python3




# initializing list
test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using map() and lambda to filter out rows of len K
res = list(filter(lambda x: len(x) == K, test_list))
 
# printing result
print("The filtered rows : " + str(res))
 
 
Output
The original list is : [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]] The filtered rows : [[1, 4, 6], [7, 3, 1]] 

The time complexity of this method is also O(n), where n is the number of sublists in the original list.

The auxiliary space required for this method is O(1), as it does not create any new lists.



Next Article
Python Program that prints rows from the matrix that have same element at a given index
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python Program that prints rows from the matrix that have same element at a given index
    Given a Matrix, the following article shows how rows which has similar digit at the specified index will be extracted and returned as output. Input : test_list = [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]], K = 1 Output : [[3345, 6355, 83, 938], [192, 993, 49]] Explanation
    5 min read
  • Python Program to Print a given matrix in reverse spiral form
    Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input: 1 2
    5 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 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 Print matrix in antispiral form
    Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16}; Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12
    2 min read
  • Python Program to print a number diamond of any given size N in Rangoli Style
    Given an integer N, the task is to print a number diamond of size N in rangoli style where N means till Nth number from number ‘1’. Examples: Input : 2 Output : --2-- 2-1-2 --2-- Input : 3 Output : ----3---- --3-2-3-- 3-2-1-2-3 --3-2-3-- ----3---- Input : 4 Output : ------4------ ----4-3-4---- --4-3
    3 min read
  • 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 test whether the length of rows are in increasing order
    Given a Matrix, the following program is used to test if length all rows of a matrix are in increasing order or not. If yes, it returns True, otherwise False. Input : test_list = [[3], [1, 7], [10, 2, 4], [8, 6, 5, 1, 4]] Output : True Explanation : 1 < 2 < 3 < 5, increasing lengths of rows
    4 min read
  • Python Program for Print matrix in zag-zag fashion
    Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: 1 2 3 4 5 6 7 8 9 Output: 1 2 4 7 5 3 6 8 9 Method 1:Approach of Python3 code This approach is simple. While travelling the matrix in the usual fashion, on basis of parity of
    3 min read
  • Python Program to find transpose of a matrix
    Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[][] is obtained by changing A[i][j] to A[j][i]. For Square Matrix: The below program finds transpose of A[][] and stores the result in B[][], we can change N for different dimension. C/C
    5 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