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: 
Using your code, here's a neater approach:

```
# Initializing Dictionary
d = {}
with open(sys.argv[1], 'r') as f:

    # counting number of times each word comes up in list of words (in dictionary)
    for line in f: 
        words = line.lower().split() 
        # Iterate over each word in line 
        for word in words: 
            if word not in d.keys():
                d[word] = 1
            else:
                d[word]+=1

n_all_words = sum([k.values])

# Print percentage occurance
for k, v in d.items():
    print(f'{k} occurs {v} times and is {(100*v/n_all_words):,.2f}% total of words.')

# Sort a dictionary using this useful solution
# https://stackoverflow.com/a/613218/10521959
import operator
sorted_d = sorted(d.items(), key=operator.itemgetter(1))

```
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()

```