Python Program to Find Most Common Elements in List

  • Post author:
  • Post category:Python
  • Reading time:1 mins 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:

Vinish Kapoor

An Oracle Apex Consultant, Oracle ACE, and founder of foxinfotech.in and orclqa.com a question and answer forum for developers.