How do I judge whether the key exists in the typed dict passed to the jit function

Hi, I’m new to numba.
It seems that if in dict. keys () cannot be used to determine whether there is a key in the dictionary. For example, the following code will report an error.

from numba import njit
from numba.core import types
from numba.typed import Dict, List

d = Dict.empty(key_type=types.unicode_type, value_type=types.ListType(types.int64))

d[‘posx’] = List([1, 5, 2])
d[‘posy’] = List([5, 3, 2])

@njit
def printdict(d):
if ‘posx’ in d.keys():
print('posx: ', d[‘posx’])
print('posy: ', d[‘posy’])

printdict(d)

Dear @Ayem

Just do what you would also do in Python:

from numba import njit
from numba.core import types
from numba.typed import Dict, List

d = Dict.empty(key_type=types.unicode_type, value_type=types.ListType(types.int64))

d['posx'] = List([1, 5, 2])
d['posy'] = List([5, 3, 2])

@njit
def printdict(d):
    if 'posx' in d:
        print('posx: ', d['posx'])
        print('posy: ', d['posy'])

printdict(d)
1 Like

Thanks :kissing_heart:,I’m too silly :smiling_face_with_tear:.