To find out how many values are populated or empty (None) in an array in Python, you can use a combination of the len()
function and a list comprehension. Here's an example:
main.py235 chars8 lines
This code creates an array my_array
with a mix of populated values and None
. The list comprehensions iterate over the array and create new lists containing only the non-empty or empty values. The lengths of these lists are then calculated using the len()
function, giving you the count of populated and empty values in the array.
Output:
main.py34 chars3 lines
This example assumes that None
represents an empty value in your array. If your array contains other custom-defined "empty" values, you can modify the list comprehensions accordingly.
gistlibby LogSnag