Logo Questions Linux Laravel Mysql Ubuntu Git Menu

dict_key object does not support indexing-python 3

I am getting an error saying "dict_key object does not support indexing" at:

return len(G[G.keys()[0]])

I realised it used to work in python 2.7.x but not in python 3.How should i change this statement to make it work in python 3.

like image 794
user3258267 Avatar asked Mar 19 '23 00:03


1 Answers

In Python 2.x, G.keys() returns a list, but Python 3.x returns a dict_keys object instead. The solution is to wrap G.keys() with call to list(), to convert it into the correct type:

return len(G[list(G.keys())[0]])
like image 115
GoBusto Avatar answered Mar 26 '23 03:03