111

I'm following instructions for a class homework assignment and I'm supposed to look up the top 200 most frequently used words in a text file.

Here's the last part of the code:

fdist1 = FreqDist(NSmyText)
vocab=fdist1.keys()
vocab[:200]

But when I press enter after the vocab 200 line, it returns:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object is not subscriptable

Any suggestions on how to fix this so it can correctly return an answer?

3
  • 2
    The stack trace does not match the code you have shown. Can you copy paste the exact code, and the error message?
    – karthikr
    Commented Oct 16, 2014 at 1:23
  • Is there a way to copy and paste text from/into the terminal? So far I have not been able to figure it out. Commented Oct 16, 2014 at 1:34
  • Need the import line to make sense of the snippet.
    – MarkHu
    Commented Mar 26, 2020 at 19:00

7 Answers 7

184

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())[:200]

In some situations it is desirable to continue working with an iterator object instead of a list. This can be done with itertools.islice():

import itertools
vocab_iterator = itertools.islice(fdist1.keys(), 200)
2
  • 18
    You might as well have written vocab = list(fdist1)
    – panda-34
    Commented Dec 18, 2014 at 18:35
  • 2
    Iterables are the basis of list operations in python3 and they need to be completed by list(iterable) : that is common not rare. I complain about a language to try to learn how to better adapt to the limitations. Commented Apr 19, 2021 at 14:58
14

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)]

1
  • 2
    imho this is a much more helpful response because it provides an example of the most_common() function which is what OP asked for.
    – GDB
    Commented Feb 16, 2018 at 15:44
2

If you want to get elements as keys and values (word and frequency), you can use:

list(fdist1.items())[:200]

1

To print the most frequently used 200 words use: fdist1.most_common(200) The above line of code will return the 200 most frequently used words as key-frequency pair.

1

I got the same error below:

TypeError: 'dict_items' object is not subscriptable

TypeError: 'dict_keys' object is not subscriptable

TypeError: 'dict_values' object is not subscriptable

When running items()[0], keys()[0] and values()[0] with the dictionary person as shown below:

person = {'name':'John', 'age':35}

print(person.items()[0]) # Error
print(person.keys()[0]) # Error
print(person.values()[0]) # Error

So, I use list() as shown below, then the error is solved:

person = {'name':'John', 'age':35}

print(list(person.items())[0]) # ('name', 'John')
print(list(person.keys())[0]) # name
print(list(person.values())[0]) # John
0

If your using python 3 try:

fdist1.most_common(200)

instead, to get the 200 most frequent words.

1
  • This is a misleading answer. this is the "collection" library and not mentioned here how to import.. I don't do downvotes but either give a full example or a link. Commented Jun 1, 2023 at 15:13
0

fdist1 = FreqDist(NSmyText)

vocab=fdist1.keys()

This code is using in Python2.7. So you should do some change. dic.keys() returns an iteratable. So using:

list(fdist1.keys())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.