Counting the Number of keywords in a dictionary in python

Dan picture Dan · Feb 6, 2010 · Viewed 488.1k times · Source

I have a list of words in a dictionary with the value = the repetition of the keyword but I only want a list of distinct words so I wanted to count the number of keywords. Is there a way to count the number of keywords or is there another way I should look for distinct words?

Answer

YOU picture YOU · Feb 6, 2010
len(yourdict.keys())

or just

len(yourdict)

If you like to count unique words in the file, you could just use set and do like

len(set(open(yourdictfile).read().split()))