close

[Solved] NLTK python : “Type: ‘dict_keys’ object is not subscriptable”

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error NLTK python : “Type: ‘dict_keys’ object is not subscriptable” in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How NLTK python : “Type: ‘dict_keys’ object is not subscriptable” Error Occurs?

Today I get the following error NLTK python : “Type: ‘dict_keys’ object is not subscriptable” in python.

How To Solve NLTK python : “Type: ‘dict_keys’ object is not subscriptable” Error ?

  1. How To Solve NLTK python : “Type: 'dict_keys' object is not subscriptable” Error ?

    To Solve NLTK python : “Type: 'dict_keys' object is not subscriptable” Error Further,if you just want to show those top 50 words not with their frequency,you can try :

  2. NLTK python : “Type: 'dict_keys' object is not subscriptable”

    To Solve NLTK python : “Type: 'dict_keys' object is not subscriptable” Error Further,if you just want to show those top 50 words not with their frequency,you can try :

Solution 1


Looks like you are using Python 3. In Python 3 dict.keys() returns an iterable but not indexable object. The most simple (but not so efficient) solution would be:

vocab = list(fdist1.keys())

Solution 2

I am using python 3.5 and I meet the same problem of  TypeError.

Using vocab = list(fdist1.keys()) does not give me the top 50 most frequently used words.
But fdist1.most_common(50) does.

Further,if you just want to show those top 50 words not with their frequency,you can try :

[word for (word, freq) in fdist1.most_common(50)]

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read