Python

How do you count repeated words in a list in Python?

In this post, we will talk about how to count repeated words in python list. It can be done in many ways.

Using collections.Counter()

# Importing counter function. 
from collections import Counter 
words = ["a", "b", "a", "c", "c", "a", "c"] 
duplicate_dict = Counter(words) 
print(duplicate_dict)#to get occurence of each of the element. 
print(duplicate_dict['a'])# to get occurence of specific element.

Output:

Counter({‘a’: 3, ‘c’: 3, ‘b’: 1})

3

  • Using count()
letter = ["b", "a", "a", "c", "b", "a", "c",'a'] 
counting=letter.count('a') 
print(counting) 

Output:

> 4

Hope this helps!

Important Notice for college students

If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com

For more Programming related blogs Visit Us Geekycodes. Follow us on Instagram.

Leave a Reply

%d bloggers like this: