How to call a dictionary from my first function in my second function in Python
<p>I am pretty new to Python and to help new learn, I am building a program, which I want broken down into 2 steps:</p>

<p>Step 1) Count the number of a particular words in a text file, store that in a dictionary where the key, value pairs are {word, count}</p>

<p>Step 2) Order the dictionary from (1) in descending order, to show the top 100 words</p>

<p>Step 1 works fine but in attempting step 2, I am struggling to call the dictionary from the first function. I create a new variable 'tallies' but this is a tuple and shows only the first entry in the dictionary.</p>

<p>How do I call the full dictionary to my 2nd function?</p>

<p>Thanks.</p>

<pre><code>filename = 'nameoffile.txt'

def tally():
  file = open(filename,'r')
  wordcount={}
  for word in file.read().split():
    if word not in wordcount:
      wordcount[word] = 1
    else:
      wordcount[word] += 1
  for k,v in wordcount.items():
    return k,v

def Count():
  tallies = tally()
  print tallies

Count()
</code></pre>

<p>These tasks are exactly what <a href="https://docs.python.org/3/library/collections.html#collections.Counter" rel="nofollow"><code>collections.Counter()</code></a> is for. You can use this function in order to create a counter-dictionary object contains words and their frequency, you can call it on splited text. Then use <code>Counter.most_common(N)</code> to get most N common items.</p>

<p>And regarding your code in following part:</p>

<pre><code>for k,v in wordcount.items():
    return k,v
</code></pre>

<p>After first iteration you are breaking the loop by <code>return</code> and it only will return the first item.</p>

<p>You can simply return the dictionary:</p>

<pre><code>def tally():
    file = open(filename,'r')
    wordcount={}
    for word in file.read().split():
        if word not in wordcount:
            wordcount[word] = 1
        else:
            wordcount[word] += 1
    return wordcount
</code></pre>

<p>You even could use <code>collections.defaultdict()</code> in order to create your counter object manually. The benefit of using this function is that it overrides one method and adds one writable instance variable.</p>

<pre><code>from collections import defaultdict

wordcount = defaultdict(int) # default is 0

def tally():
    with open(filename) as f 
    for word in f.read().split():
            wordcount[word] += 1
    return wordcount
</code></pre>

<p>And for returning the sorted items you can use <code>sorted()</code> function on dictionary items by passing a key function to it, to say that sort the items by second item. For example:</p>

<pre><code>sorted(wordcount.items(), key=lambda x:x[1])
</code></pre>

<p>But as I said the the first, the pythonic and optimized approach is using <code>collections. Counter()</code>.</p>

<pre><code>from collections import Counter

with open(filename) as f:
    wordcount = Counter(f.read().split())

top100 = wordcount.most_common(100)
</code></pre>

python dictionary