• Don't use is to compare if two objects are equal, use == instead:
In [110]: [] is []
Out[110]: False

In [111]: []==[]
Out[111]: True
  • Your g is a 1D numpy array, and g[j] gets you an element, whilst curr_g might be an array. To reproduce your error:
In [117]: l=np.arange(3)
     ...: l[0]=[1,2] #assigning an element of a numpy array to as a python list
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-117-817e72d77bd0> in <module>()
      1 l=np.arange(3)
----> 2 l[0]=[1,2]

ValueError: setting an array element with a sequence.
  • np.array([no_classes, no_features, no_classes]) gives you a 1D array of length 3. If you want to initialize g as a 3D array of shape (no_classes, no_features, no_classes), use np.zeros, e.g.:
In [124]: np.zeros((2,3,2))
Out[124]: 
array([[[ 0.,  0.],
        [ 0.,  0.],
        [ 0.,  0.]],

       [[ 0.,  0.],
        [ 0.,  0.],
        [ 0.,  0.]]])
  • To check if a subarray of g is unassigned(namely, all are 0s here IIUC), use np.allclose:
In [128]: l=np.zeros((2,3,2))

In [129]: np.allclose(l[0], 0)
Out[129]: True
Answer from zhangxaochen on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.4 Manual
Return a new array of given shape and type, without initializing entries · Shape of the empty array, e.g., (2, 3) or 2
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-183.php
Python NumPy: Test whether a given 2D array has null columns or not - w3resource
Write a NumPy program to check if any column in a 2D array consists entirely of zeros using np.all and axis=0. Create a function that returns a boolean for each column indicating whether it is null (all zeros).
🌐
Appdividend
appdividend.com › create-and-check-if-a-numpy-array-is-empty
How to Create and Check If a Numpy Array is Empty
July 10, 2025 - ... import numpy as np arr3d = np.empty((2, 0, 4)) # 2 rows 0 columns 4 depth print(arr3d) print(arr3d.size) print(arr3d.shape) # Output: # [] # 0 # (2, 0, 4) The most Pythonic way to check if an array is empty is to use the .size attribute.
🌐
Eileen's Lounge
eileenslounge.com › viewtopic.php
Check if 2D array is empty or not - Eileen's Lounge
I'd use a Boolean variable instead. In the loop, set the variable to True when an element is NOT empty, and immediately exit the loop. If the variable is still False after the loop, all array elements were empty.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.2 Manual
Return a new array of given shape and type, without initializing entries · Shape of the empty array, e.g., (2, 3) or 2
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 160559 › empty-2d-array
python - Empty 2D Array [SOLVED] | DaniWeb
So hoe to write a generic code for creating a empty 2D array and dynamically insert values in it. ... When your row lengths vary, think of a 2D structure as a list of independent rows (a ragged list). You can start empty and append each row as its length becomes known, initializing with a placeholder such as None so you can fill values later: row_lengths = [len1, len2, ...] # discovered at runtime grid = [] for row_len in row_lengths: grid.append([None] * row_len) # each row is a distinct list
🌐
Sling Academy
slingacademy.com › article › numpy-test-if-an-array-is-empty-or-not-3-examples
NumPy: Test if an array is empty or not (3 examples) - Sling Academy
This method is particularly useful for multi-dimensional arrays. import numpy as np # Creating a 2D empty array empty_2d_array = np.empty((0,2)) # Checking for emptiness if np.size(empty_2d_array) == 0: print('This 2D array is empty.') else: print('This 2D array has elements.')
Find elsewhere
🌐
Python Guides
pythonguides.com › check-if-numpy-array-is-empty
Check If NumPy Array Is Empty In Python
May 16, 2025 - # Using any() with 0 in shape (more reliable) is_empty_better = 0 in empty_array.shape print(f"Is empty (using 0 in shape): {is_empty_better}") # True # For multidimensional arrays empty_2d = np.zeros((0, 5)) is_empty_2d = 0 in empty_2d.shape print(f"Is 2D array empty: {is_empty_2d}") # True · Be careful with the direct use of any() or all() as they can be tricky with one-dimensional arrays, checking for zero in the shape is more reliable. Read NumPy Reset Index of an Array in Python ·
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.1 Manual
Return a new array of given shape and type, without initializing entries · Shape of the empty array, e.g., (2, 3) or 2
🌐
Quora
quora.com › How-do-I-check-if-an-array-is-empty-in-Python
How to check if an array is empty in Python - Quora
Answer (1 of 12): I am going to assume you are talking about lists (Python does have arrays, but they are very different to lists). Three ways : 1 Test the truthiness If you know the item is a list you do : [code]if not my_list: print(‘List is empty’) [/code]Empty containers (lists,sets,t...
🌐
GitHub
gist.github.com › JoshK2 › 76edc39618227a6365596b52490ab67d
Check if 2d array have an empty cell · GitHub
Check if 2d array have an empty cell. GitHub Gist: instantly share code, notes, and snippets.
🌐
TutorialsPoint
tutorialspoint.com › what-is-the-preferred-method-to-check-for-an-empty-array-in-numpy
What is the preferred method to check for an empty array in NumPy?
In this method, we used the inputArray.size attribute to determine whether or not the array is empty. This operator returns the array size, which in this example is 0, resulting in the expected result. This is a numpy array attribute that gives a tuple containing the array's shape. This can be used to see if the array is empty.
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-95.php
NumPy: Check whether the numpy array is empty or not - w3resource
# Importing the NumPy library and aliasing it as 'np' import numpy as np # Creating a NumPy array 'x' containing integers [2, 3] x = np.array([2, 3]) # Creating an empty NumPy array 'y' y = np.array([]) # Printing the size of array 'x' # As 'x' contains 2 elements, its size is 2 print(x.size) # Printing the size of array 'y' # 'y' is an empty array, so its size is 0 print(y.size)
🌐
w3resource
w3resource.com › numpy › array-creation › empty.php
NumPy: numpy.empty() function - w3resource
Explanation: This code demonstrates how to use np.empty() function in NumPy to create empty arrays with specified data types. The dtype parameter in the np.empty() function can be used to specify the data type of the empty array.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.5.dev0 Manual
Return a new array of given shape and type, without initializing entries · Shape of the empty array, e.g., (2, 3) or 2
🌐
Data Science Parichay
datascienceparichay.com › home › blog › how to check if a numpy array is empty?
How to Check if a Numpy Array is Empty? - Data Science Parichay
November 28, 2022 - To check if a numpy array is empty, use the array's .size attribute and check if it's equal to 0 (an array's size is zero if it has no elements).
🌐
Python Pool
pythonpool.com › home › blog › 5 ways to check if the numpy array is empty
5 Ways to Check if the NumPy Array is Empty - Python Pool
January 1, 2024 - As the array is empty, the flag variable’s value becomes True, so the output ‘Array is empty’ is displayed. The limitation to this function is that it does not work if the array contains the value 0 in it. We use the numpy.size() function in Python to count the number of elements along a given axis.
🌐
GeeksforGeeks
geeksforgeeks.org › python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
Hence the better way to declare a 2d array is ... This method creates 5 separate list objects, unlike method 2a. One way to check this is by using the 'is' operator which checks if the two operands refer to the same object.
Published   June 20, 2024
🌐
IncludeHelp
includehelp.com › python › how-to-check-whether-a-numpy-array-is-empty-or-not.aspx
How to check whether a NumPy array is empty or not?
The numpy.any() method checks whether any array element along a given axis evaluates to True. If the given array is not empty it returns True; False, otherwise. numpy.any(a, axis=None, out=None, keepdims=<no value>, *, where=<no value>) Consider the below Python program to check NumPy array ...