Python Word Count (Filter out Punctuation, Dictionary Manipulation, and Sorting Lists)
For the text below, count how many times each word occurs.
Text="""
bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation. We hold these truths to be
self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.--That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed, --That whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to effect their Safety and Happiness. """
Clean Punctuation and Transform All Words to Lowercase.
# Cleaning text and lower casing all words
for char in '-.,\n':
Text=Text.replace(char,' ')
Text = Text.lower()# split returns a list of words delimited by sequences of whitespace (including tabs, newlines, etc, like re's \s)
word_list = Text.split()

Output a List of Word Count Pairs (Sorted from Highest to Lowest)
Approach 1: Collections Module
This is the easiest way to do this, but it requires knowing which library to use.
from collections import CounterCounter(word_list).most_common()
Approach 2: Using For Loops
This approach uses the dictionary method get. If you want to learn more about using the method, see the tutorial Python Dictionary and Dictionary Methods.
# Initializing Dictionary
d = {}
# counting number of times each word comes up in list of words (in dictionary)
for word in word_list:
d[word] = d.get(word, 0) + 1
Next, reverse the key and values so they can be sorted using tuples.
word_freq = []
for key, value in d.items():
word_freq.append((value, key))
Next, sort from highest to lowest.
word_freq.sort(reverse=True)
print(word_freq)
Approach 3: Not using Dictionary Get Method
# Initializing Dictionary
d = {}
# Count number of times each word comes up in list of words (in dictionary)
for word in word_list:
if word not in d:
d[word] = 0
d[word] += 1
Next, reverse the key and values so they can be sorted using tuples.
word_freq = []
for key, value in d.items():
word_freq.append((value, key))
word_freq.sort(reverse=True)
Approach 4: Using sorted
# initializing a dictionary
d = {};
# counting number of times each word comes up in list of words
for key in word_list:
d[key] = d.get(key, 0) + 1
sorted(d.items(), key = lambda x: x[1], reverse = True)
Concluding Remarks
Using any of the four methods should give you an output like

I should note that the code used in this blog post and in the video above is available on my github. Please let me know if you have any questions either here, on youtube, or through Twitter! If you want to learn how to utilize the Pandas, Matplotlib, or Seaborn libraries, please consider taking my Python for Data Visualization LinkedIn Learning course.