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)}
Similar to Decency's answer, but taking advantage of the object_hook
argument:
coautorshipDictionary = json.load(json_data, object_hook=lambda d: {int(k): [int(i) for i in v] if isinstance(v, list) else v for k, v in d.items()}) # iteritems() for Python 2
The main advantage of this method is that, if you ever end up with any nested dicts, the loader will handle each nested dict on its own as it loads the data without you having to write code to walk through your result dict. You could also add in checks for cases where values in lists are not numeric strings or the lists themselves contain dicts as well, if your JSON structure gets more complicated, and if your data will only have lists as the values for your top-level dict you can remove the if isinstance(v, list) else v
part.