Press "Enter" to skip to content

Python Program to Find Most Common Elements in List

Here is a Python program to find the most common elements in a list.

Python Program to Find Most Common Elements (Repeated Values) in a List

words = [
   'see', 'into', 'my', 'eyes', 'see', 'into', 'my', 'eyes',
   'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
   'eyes', "don't", 'see', 'around', 'the', 'eyes', 'see', 'into',
   'my', 'eyes', "you're", 'under'
]

from collections import Counter
word_counts = Counter(words)
top_three = word_counts.most_common(3)
print(top_three)

morewords = ['why','are','you','not','seeing','in','my','eyes']
word_counts.update(morewords)
print(word_counts.most_common(3))

Output:

[('eyes', 8), ('the', 5), ('see', 4)]
[('eyes', 9), ('the', 5), ('see', 4)]

Process finished with exit code 0

See also:

Vinish Kapoor

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *