Search code examples
pythonnumba

How to use lists within lists in Numba?


I'm just learning Numba and am a bit stuck with this. When I run this code it gives

Encountered the use of a type that is scheduled for deprecation: type 'reflected list' found for argument 'item' of function '_append'.

I've looked around to try to find a solution to fix it but I can't find much.

from numba import njit
from numba.typed import List

@njit
def foo (b):
    for y in range(3):
        if b[y][0] == 1:
            return True
        return False
a = [[1, 2, 3],[1,1,1]]
typed_a = List(a)
foo(typed_a)

I want to be able to index lists within lists with Numba, the code is just an example code but I think if I can get that code to work, everything should work out.

I have tried everything I could find but I'm just not very knowledgeable about Numba and was hoping someone could help me out.

Thanks so much!


Solution

  • numba doesn't support reflected list, which are lists of arbitrary types, you can overcome this by passing in a numba list of numba lists of a certain type, by simply calling List(List(x) for x in a) as follows.

    from numba import njit
    from numba.typed import List
    @njit
    def foo(b):
        for y in range(3):
            if b[y][0] == 1:
                return True
            return False
    a = [[1, 2, 3],[1,1,1]]
    typed_a = List(List(x) for x in a)  # make a numba list of numba lists.
    foo(typed_a)
    

    converting the input to a numpy array is another solution, which would have better performance, there's hardly a reason to use lists in numba, as lists are slow compared to numpy arrays due to the conversion from normal lists to "numba lists", lists are only useful if you want a container that you can append and pop on cheaply

    to use numpy arrays instead you just need to do:

    import numpy as np
    typed_a = np.array(a)