Convert a string key to int in a Dictionary

d = {'1':'145' , '2':'254' , '3':'43'}
d = {int(k):int(v) for k,v in d.items()}
>>> d
{1: 145, 2: 254, 3: 43}

for lists in values

>>> d = { '1':['1', '2', '3', '4'] , '2':['1', '4'] , '3':['43','176'] }
>>> d = {int(k):[int(i) for i in v] for k,v in d.items()}

in your case:

coautorshipDictionary = {int(k):int(v) for k,v in json.load(json_data)}

or

coautorshipDictionary = {
    int(k):[int(i) for i in v] for k,v in json.load(json_data)}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)