Question: I'm new to python and I want to Count the number of times each word occurs across all the files. Display each word, the number of times it occurred and the percentage of time it occurred. Sort the list so the most frequent word appears first, and the least frequent word appears last.
I'm working on small sample right know just one file but I can't get to work right, 

```
 from collections import defaultdict

words = "apple banana apple strawberry banana lemon"

d = defaultdict(int)
for word in words.split():
    d[word] += 1

```

Answer: 
As mentioned in the comments, this is precisely `collections.Counter`

```
words = 'a b c a'.split()
print(Counter(words).most_common())

```

From docs: <https://docs.python.org/2/library/collections.html>

```
most_common([n])
Return a list of the n most common elements and their counts
from the most common to the least. If n is omitted or None,
most_common() returns all elements in the counter.
Elements with equal counts are ordered arbitrarily:

>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]

```
the most straightforward way to do this is just using the Counter function:

```
from collections import Counter
c = Counter(words.split())

```

output:

```
Counter({'apple': 2, 'banana': 2, 'strawberry': 1, 'lemon': 1})

```

to just get the words in order, or the counts:

```
list(c.keys())
list(c.values())

```

or put it into a normal dict:

```
dict(c.items())

```

or list of tuples:

```
c.most_common()

```