Home > other >  TypeError: 'dict_keys' object is not callable
TypeError: 'dict_keys' object is not callable

Time:01-07

I have used the below program to get the maximum and minimum value in a dictionary. But, there is an type error.

my_dict = {'x':500, 'y':5874, 'z': 560}

key_max = max(my_dict.keys(), key=(lambda k: my_dict[k]))

key_min = min(my_dict.keys(), key=(lambda k: my_dict[k]))

print('Maximum Value: ',my_dict[key_max])

print('Minimum Value: ',my_dict[key_min])

(source - https://www.w3resource.com/python-exercises/dictionary/python-data-type-dictionary-exercise-15.php)

I am running this code on Jupyter notebook and getting the type error as the 'dict.keys' object is not callable.

The same code runs successfully on the pycharm.

Please advise me what changes should I make so that the code runs successfully on the jupyter notebook? what is wrong in my code?code run on JupyterNBcode run on Pycharm successfully](https://i.stack.imgur.com/UntNd.png)

CodePudding user response:

I have tested your program in Jupyter Notebook and the code is working just fine. Maybe you check for any typos. Hope this help

Code snippet run on jupyter notebook

CodePudding user response:

Yup I can run your code perfectly too. Please try to simplify it like below. It looks better too.

my_dict = {'x':500, 'y':5874, 'z': 560}

key_max = max(my_dict, key = my_dict.get)

key_min = min(my_dict, key = my_dict.get)

print('Maximum Value: ',my_dict[key_max])

print('Minimum Value: ',my_dict[key_min])
  • Related