How To Use Integer Sequence As Key of Typed Dict

I need to set an array of integers to typed dict as the key. In numba 0.53.1, I find it seems that only tuple can work as the key but the tuple can’t be constructed by List or numpy.array, so I have not way to input my data to the Tuple.

Is there any way to handle this case? (Make integer sequence as typed dict key)

Any comments will be welcomed and appreciated.

Hi @cuauty

Do you know the length of your arrays ahead of time, and do they have a reasonable length, say <10 elements?
Then the easiest way would be to construct the tuple manually from the array data.

If you want to use tuples numba needs to know their length at compile time, which is impossible with Python’s very flexible tuple() constructor, that is likely why it is not supported in numba.

If you hardcode a tuple construction like

tup = (arr[0], arr[1], arr[2])

then numba can infer the tuple length and compile successfully.

Cheers
Hannes

Somthing that should work, but feels a bit like an abomination, is to construct a string from the array data, e.g.

from numba import njit
import numpy as np

@njit
def stringify(arr):
    return ",".join([str(n) for n in arr])
>>>stringify(np.arange(5))
'0,1,2,3,4'

I think the following post may be related: How to use a sequence as dict key? - #8 by stuartarchibald

1 Like

Yes, it works as workaround for my case. The issue is, I am not sure how impact to performance due to the str.join. Thanks a lot for your reply.

Many thanks for your valuable help, and I would like to talk more in that thread.

In general, I think that constructing Tuple with List or array should be common requirement, no matter whether use as key of dict.

I am not a numba dev, but from what I understand:

Constructing a tuple in jitted code is absolutely possible as in my example above (but maybe a bit less convenient than in conventional Python).
But there is the limitation that the type of the tuple has to be well defined at compilation time and its type cannot drift between calls to the function.
Since arrays and lists have undefined lengths / shapes it is simply not possible to infer what a tuple created from them would have to look like / how much memory it would need, unless you explicitly spell it out.
If you pass tuples with different lengths or member types to a function as an argument, that will trigger a recompilation of the function for a new call signature, like an overload in a strongly typed language.
This is not a limitation of numba but of static typing.

I am fully with you, it feels dodgy at best (but I haven’t benchmarked anything). The ideas in the thread @esc linked are probably better.