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 - Check for None value in Matrix
Next article icon

Python - Flag None Element Rows in Matrix

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

Given a Matrix, return True for rows which contain a None Value, else return False.

Input : test_list = [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8, None]] 
Output : [True, True, False, True] 
Explanation : 1, 2 and 4th index contain None occurrence. 

Input : test_list = [[2, 4, None, 3], [3, 4, 1, None], [2, 4, None, 4], [2, 8, None]] 
Output : [True, True, True, True] 
Explanation : All rows have None.

Method #1: Using List comprehension

In this, we iterate for each row and use in operator to check for None in these. If found, True is returned, else False is returned.

Python3
# Python3 code to demonstrate working of  # Flag None Element Rows in Matrix # Using list comprehension  # initializing list test_list = [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]]  # printing original list print("The original list is : " + str(test_list))  # in operator to check None value  # True if any None is found  res = [True if None in sub else False for sub in test_list]  # printing result  print("None Flagged List : " + str(res)) 

Output
The original list is : [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]] None Flagged List : [True, True, False, False]

Time Complexity: O(n), where n is the length of the input list. 
Auxiliary Space: O(1) additional space is not required

Method #2: Using all() + list comprehension

In this, we check for all values to be True using all(), if yes, its not flagged True and list comprehension is used to check for each row.

Python3
# Python3 code to demonstrate working of  # Flag None Element Rows in Matrix # Using all() + list comprehension  # initializing list test_list = [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]]  # printing original list print("The original list is : " + str(test_list))  # all() checks for all non none values   res = [False if all(sub) else True for sub in test_list]  # printing result  print("None Flagged List : " + str(res)) 

Output
The original list is : [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]] None Flagged List : [True, True, False, False]

Method #3: Using count() method

In this, we will find the count() of None in all the rows, if the count is greater or equal to 1, then the row is flagged True else False.

Python3
# Python3 code to demonstrate working of # Flag None Element Rows in Matrix  # initializing list test_list = [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]]  # printing original list print("The original list is : " + str(test_list)) res = [] for i in test_list:     if i.count(None) >= 1:         res.append(True)     else:         res.append(False)  # printing result print("None Flagged List : " + str(res)) 

Output
The original list is : [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]] None Flagged List : [True, True, False, False]

Method #4:Using a for loop with an if condition

Python3
test_list = [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]]  print("The original list is : " + str(test_list))  res = [] for sub in test_list:     if None in sub:         res.append(True)     else:         res.append(False)  print("None Flagged List : " + str(res)) #This code is contributed by Vinay pinjala. 

Output
The original list is : [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]] None Flagged List : [True, True, False, False]

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

Method 5: Using the any() function with a generator expression.

Step-by-step approach:

  • Initialize an empty list to store the results.
  • Use the any() function with a generator expression to check if any sublist contains a None value. The generator expression should iterate over the sublists in the test_list and check if None is present in each sublist.
  • Append the result of each check to the result list.
  • Print the result list.
Python3
test_list = [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]] print("The original list is : " + str(test_list))  # Method 5: Using any() function with a generator expression res = [any(val is None for val in sub) for sub in test_list]  print("None Flagged List : " + str(res)) 

Output
The original list is : [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8]] None Flagged List : [True, True, False, False]

Time complexity: O(n*m), where n is the number of sublists and m is the average length of each sublist.
Auxiliary space: O(n), where n is the number of sublists.


Next Article
Python - Check for None value in Matrix
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python-Matrix
Practice Tags :
  • python

Similar Reads

  • Python | Row with Minimum element in Matrix
    We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
    5 min read
  • Search Elements in a Matrix - Python
    The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1
    3 min read
  • Python - Check Similar elements in Matrix rows
    Given a Matrix and list, the task is to write a Python program to check if all the matrix elements of the row are similar to the ith index of the List. Input : test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Output : True Explanation : All rows have same elements.Input : test_list = [[1, 1,
    8 min read
  • Python - Check for None value in Matrix
    Python supports a list as its list element and hence a matrix can be formed. Sometimes we might have a utility in which we require to perform None check in that list of list i.e matrix and its a very common in all the domains of coding, especially Data Science. Let’s discuss certain ways in which th
    5 min read
  • Handling " No Element Found in Index() " - Python
    The task of handling the case where no element is found in the index() in Python involves safely managing situations where the desired element does not exist in a list. For example, with the list a = [6, 4, 8, 9, 10] and the element 11, we want to ensure the program doesn't raise an error if the ele
    3 min read
  • Python | Remove last element from each row in Matrix
    Sometimes, while working with Matrix data, we can have a stray element attached at rear end of each row of matrix. This can be undesired at times and wished to be removed. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + del + list slicing The combination of th
    6 min read
  • Python | Search in Nth Column of Matrix
    Sometimes, while working with Python Matrix, we can have a problem in which we need to check if an element is present or not. This problem is simpler, but a variation to it can be to perform a check in a particular column of Matrix. Let's discuss a shorthand by which this task can be performed.  Met
    10 min read
  • Python - Filter rows with Elements as Multiple of K
    Given a Matrix, extract rows with elements multiple of K. Input : test_list = [[5, 10, 15], [4, 8, 12], [100, 15], [5, 10, 23]], K = 4 Output : [[4, 8, 12]] Explanation : All are multiples of 4. Input : test_list = [[5, 10, 15], [4, 8, 11], [100, 15], [5, 10, 23]], K = 4 Output : [] Explanation : No
    6 min read
  • Python | Remove similar element rows in tuple Matrix
    Sometimes, while working with data, we can have a problem in which we need to remove elements from the tuple matrix on a condition that if all elements in row of tuple matrix is same. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + all() This ta
    6 min read
  • Python - Sort Matrix by None frequency
    In the world of Python programming, many developers aim to make matrix operations efficient and elegant in their code. A fascinating challenge that comes up is sorting a matrix based on how often the mysterious "None" element appears, adding an interesting twist to data manipulation in Python. Given
    9 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