views:

146

answers:

3

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 to count the number of keywords or is there another way I should look for distinct words?

+1  A: 

The number of distinct words (i.e. count of entries in the dictionary) can be found using the len() function.

> a = {'foo':42, 'bar':69}
> len(a)
2

To get all the distinct words (i.e. the keys), use the .keys() method.

> list(a.keys())
['foo', 'bar']
KennyTM
very nice. thanks a lot for the help!
Dan
A: 

I cannot understand your explanation. I think you might want to use a set, not a list. A set is an unordered collection of unique elements.

Mike Graham
+2  A: 
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()))
S.Mark