how to know how much values in an array are populated or none in python

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.py
my_array = [1, None, 3, None, 5, None]

populated_count = len([x for x in my_array if x is not None])
empty_count = len([x for x in my_array if x is None])

print("Populated count:", populated_count)
print("Empty count:", empty_count)
235 chars
8 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.py
Populated count: 3
Empty count: 3
34 chars
3 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.

related categories

gistlibby LogSnag