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 CounterPython tutorial Comment More infoAdvertise with us Next Article Python most_common() Function O oceanofknow6flv Follow Improve Article Tags : Python Python-Functions Python collections-module Practice Tags : pythonpython-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 : Python3 # Python program explain 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.ExamplePython# Example 1 print(cmp(3, 4)) # Example 2 print(cmp(5, 5)) # Example 3 print(cmp(7, 6)) Output-101ExplanationI 3 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 does a specific task. The idea is to put some commonly or repeatedly done task 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 over an 9 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 Like