# How to create typed lists of structured numpy arrays?

**URL:** <https://numba.discourse.group/t/how-to-create-typed-lists-of-structured-numpy-arrays/2696>\
**Category:** Support: How do I do ...?\
**Created:** [August 22, 2024, 11:59am UTC](https://numba.discourse.group/t/how-to-create-typed-lists-of-structured-numpy-arrays/2696 "2024-08-22T11:59:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lak](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@lak](https://numba.discourse.group/u/lak)\
**Post date:** [August 22, 2024, 11:59am UTC](https://numba.discourse.group/t/how-to-create-typed-lists-of-structured-numpy-arrays/2696/1 "2024-08-22T11:59:32Z")

</div>

Hi,

I am trying to create typed lists for structured NumPy arrays. Creating these outside of `@njit` works just fine, but creating these inside of an `@njit` function throws the following error:

> TypingError: Failed in nopython mode pipeline (step: nopython frontend)  
> Invalid use of \<class ‘numba.core.types.npytypes.Array’\> with parameters (typeref[Record(node\_y\_values[type=float64;offset=0],left\_child\_pointers[type=int32;offset=8],right\_child\_pointers[type=int32;offset=12];16;True)], Literalint, Literalstr)  
> No type info available for \<class ‘numba.core.types.npytypes.Array’\> as a callable.  
> During: resolving callee type: typeref[\<class ‘numba.core.types.npytypes.Array’\>]

Example code:

```auto
import numpy as np
from numba.typed import List
from numba.types import Array
from numba import from_dtype, njit
dtype = from_dtype(np.dtype([('node_y_values', np.float64),
                             ('left_child_pointers', np.int32),
                             ('right_child_pointers', np.int32)], align=True))
typed_list = List.empty_list(Array(dtype, 1, "C"))
@njit
def struc_arr_typed_list():
    typed_list = List.empty_list(Array(dtype, 1, "C"))
struc_arr_typed_list()

```

I also tried the following: typed\_list = List.empty\_list(dtype[:]), but that gives another typing error. Creating the typed list outside of @njit and then passing it into a jitted function works, but this results in unexpectedly slow performance in appending. Am I approaching this the wrong way? I could of course create separate arrays per datatype and use these, but putting structured arrays inside of a typed list is a much cleaner solution. Would greatly appreciate any tips or help on this! I am using Numba 0.59.0 and NumPy 1.26.4 in Python 3.12.2 if that helps.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![lak](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@lak](https://numba.discourse.group/u/lak)\
**Post date:** [August 22, 2024, 7:36pm UTC](https://numba.discourse.group/t/how-to-create-typed-lists-of-structured-numpy-arrays/2696/2 "2024-08-22T19:36:34Z")

</div>

If anyone stumbles across this looking for the same functionality, I figured it out. The trick is to first create the dtype, and then create an array dtype, both outside of @njit. See code below.

```auto
import numpy as np
from numba.typed import List
from numba.types import Array
from numba import from_dtype, njit
dtype = from_dtype(np.dtype([('node_y_values', np.float64),
                             ('left_child_pointers', np.int32),
                             ('right_child_pointers', np.int32)], align=True))
numba_dtype_arr = Array(dtype, 1, 'C')
@njit
def test():
    typed_list = List.empty_list(numba_dtype_arr)
    for i in range(5):
        typed_list.append(np.empty(0, dtype=dtype))
test()

```
