Consider some 2d lists:
JavaScript
x
6
1
a = [[1,2,3,4],
2
[5,6,7,None]]
3
4
b = [[1,2,3,4],
5
[5,6,7,8]]
6
How to check if there is at least one None in a 2d list?
Outputs: deal with a should output a bool value False, and b should output True.
I have no ideas when the list be a 2d list.
Advertisement
Answer
You can use two loops, one inside the other.
JavaScript
1
7
1
def at_least_one_none(array):
2
for row in array:
3
for item in row:
4
if item == None:
5
return True
6
return False
7
This can be simplified by using None in row
rather than the inner loop.
JavaScript
1
6
1
def at_least_one_none(array):
2
for row in array:
3
if None in row:
4
return True
5
return False
6
Either could be written using any()
with a generator expression:
JavaScript
1
3
1
def at_least_one_none(array):
2
return any(None in row for row in array)
3
And at that point you barely need the function.