Python – Flag None Element Rows in Matrix
Last Updated :
01 May, 2023
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
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 = [ True if None in sub else False 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), 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
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 = [ False if all (sub) else True 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]
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
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 i in test_list:
if i.count( None ) > = 1 :
res.append( True )
else :
res.append( False )
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))
|
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))
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.
Similar Reads
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
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. Meth
10 min read
Python - Non-None elements indices
Many times while working in data science domain we need to get the list of all the indices which are Non None, so that they can be easily be prepossessed. This is quite a popular problem and solution to it comes quite handy. Letâs discuss certain ways in which this can be done. Method #1: Using list
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
Python - Rows with all List elements
Given a Matrix, get all the rows with all the list elements. Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [1, 2] Output : [[2, 1, 8], [6, 1, 2]] Explanation : Extracted lists have 1 and 2. Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [2
8 min read
Python - Remove front column from Matrix
Sometimes, while working with Matrix data, we can have stray element that attached at front 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 o
6 min read