Python Program to Find Most Common Elements in List

  • Python
  • 1 min read

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: