Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
cmp() function - Python
Next article icon

Python most_common() Function

Last Updated : 17 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

most_common() function is a method provided by the Counter class in Python's collections module. It returns a list of the n most common elements and their counts from a collection, sorted by frequency. Example:

Python
from collections import Counter  a = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']  # create a Counter object counter = Counter(a)  # elements according to the frequency in decreasing order res = counter.most_common() print(res) 

Output
[('apple', 3), ('banana', 2), ('orange', 1)] 

Syntax of most_common()

most_common(n)

Parameters:

  • n (optional): An integer specifying how many of the most common elements to retrieve. Defaults to returning all elements.

Returns: A list of tuples containing the n most common elements and their counts, sorted in descending order by frequency.

Examples of most_common()

Example 1: Finding the Most Common Words in a String

Python
from collections import Counter  s = "Ram and Shyam are friends. Also, Ram and Ghanshyam are college friends. All of them Studies from GeeksforGeeks"  res = Counter(s.split()).most_common(3) print(res) 

Output
[('Ram', 2), ('and', 2), ('are', 2)] 

Explanation: The function splits the text into words and counts how many times each word appears. The three most common words are returned along with their counts.

Example 2: Counting Character Occurrences

Python
from collections import Counter  a = "abracadabra"  # Get the most common character and its count res = Counter(string).most_common(1)  print(res) 

Output
[('a', 5)] 

Explanation: The function counts the occurrence of each character in the string and returns the most common character ('a') along with its frequency (5 times).

Example 3: Identifying Popular Items in a list

Python
from collections import Counter  a = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']  res = Counter(items).most_common(1) print(res) 

Output
[('apple', 3)] 

Explanation: The function counts how many times each item appears in the list and returns the most common item ('apple') with its frequency (3 times).

Related Articles:

  • Python collections Counter
  • Python tutorial

Next Article
cmp() function - Python

O

oceanofknow6flv
Improve
Article Tags :
  • Python
  • Python-Functions
  • Python collections-module
Practice Tags :
  • python
  • python-functions

Similar Reads

  • numpy.common_type() function – Python
    numpy.common_type() function return a scalar type which is common to the input arrays. Syntax : numpy.common_type(arrays) Parameters : array1, array2, .... : [ndarrays] Input arrays. Return : [dtype] Return the data type which is common to the input arrays. Code #1 : # Python program explaining # nu
    1 min read
  • cmp() function - Python
    cmp() method in Python 2.x compares two integers and returns -1, 0, 1 according to comparison. cmp() does not work in python 3.x. You might want to see list comparison in Python. Example[GFGTABS] Python # Example 1 print(cmp(3, 4)) # Example 2 print(cmp(5, 5)) # Example 3 print(cmp(7, 6)) [/GFGTABS]
    3 min read
  • modf() function - Python
    modf() function is an inbuilt function in Python that returns the fractional and integer parts of the number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float. Example:[GFGTABS] Python import math # modf() function used with a positive number p
    4 min read
  • numpy.find_common_type() function - Python
    numpy.find_common_type() function determine common type following standard coercion rules. Syntax : numpy.find_common_type(array_types, scalar_types) Parameters : array_types : [sequence] A list of dtypes or dtype convertible objects representing arrays. scalar_types : [sequence] A list of dtypes or
    1 min read
  • Python Functions
    Python Functions is a block of statements that return the specific task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it ov
    11 min read
  • chr() Function in Python
    chr() function returns a string representing a character whose Unicode code point is the integer specified. chr() Example: C/C++ Code num = 97 print("ASCII Value of 97 is: ", chr(num)) OutputASCII Value of 97 is: a Python chr() Function Syntaxchr(num) Parametersnum: an Unicode code integer
    3 min read
  • Python - globals() function
    In Python, the globals() function is used to return the global symbol table - a dictionary representing all the global variables in the current module or script. It provides access to the global variables that are defined in the current scope. This function is particularly useful when you want to in
    2 min read
  • Python | math.gcd() function
    In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments. Syntax: math.gcd(x, y) Parameter: x : Non-negative integer whose gcd has to be comp
    2 min read
  • numpy.correlate() function - Python
    numpy.correlate() function defines the cross-correlation of two 1-dimensional sequences. This function computes the correlation as generally defined in signal processing texts: c_{av}[k] = sum_n a[n+k] * conj(v[n]) Syntax : numpy.correlate(a, v, mode = 'valid') Parameters : a, v : [array_like] Input
    1 min read
  • dir() function in Python
    The dir() function is a built-in Python tool used to list the attributes (like methods, variables, etc.) of an object. It helps inspect modules, classes, functions, and even user-defined objects during development and debugging. Syntaxdir([object]) Parameters: object (optional): Any Python object (l
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences