Open In App

Python – Flag None Element Rows in Matrix

Improve
Improve
Like Article
Like
Save
Share
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.



Last Updated : 01 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads