Altering dictionaries/keys in Python
<p>I have ran the code below in Python to generate a list of words and their count from a text file. How would I go about filtering out words from my "frequency_list" variable that only have a count of 1? </p>

<p>In addition, how would I export the print statement loop at the bottom to a CSV</p>

<p>Thanks in advance for any help provided.</p>

<pre><code>import re
import string
frequency = {}
document_text = open('Words.txt', 'r')
text_string = document_text.read().lower()
match_pattern = re.findall(r'\b[a-z]{3,15}\b', text_string)

for word in match_pattern:
    count = frequency.get(word,0)
    frequency[word] = count + 1

frequency_list = frequency.keys()



for words in frequency_list:
    print (words, frequency[words])
</code></pre>

<p>For the first part - you can use dict comprehension:</p>

<p><code>frequency = {k:v for k,v in frequency.items() if v&gt;1}</code></p>

python dictionary export-to-csv