Last updated: Apr 10, 2024
Reading timeยท8 min
To find the most common element in a list:
collections.Counter()
class to create a counter object.most_common()
method to get the most common element in the list.from collections import Counter a_list = ['one', 'one', 'one', 'two', 'two', 'three'] counter = Counter(a_list) most_common = counter.most_common(1)[0][0] print(most_common) # ๐๏ธ one most_common_2 = counter.most_common(2) print(most_common_2) # ๐๏ธ [('one', 3), ('two', 2)]
If you need to find the least common element in a list, click on the following subheading.
We used the collections.Counter
class to count the occurrences of each list
item.
The
Counter
class from the collections
module is a subclass of the dict
class.
The class is basically a mapping of key-count pairs.
from collections import Counter a_list = ['one', 'one', 'one', 'two', 'two', 'three'] counter = Counter(a_list) print(counter) # ๐๏ธ Counter({'one': 3, 'two': 2, 'three': 1})
Counter
objects implement a
most_common()
method that returns a list of the N most common elements and their counts from
the most common to the least.
from collections import Counter a_list = ['one', 'one', 'one', 'two', 'two', 'three'] counter = Counter(a_list) most_common = counter.most_common(1)[0][0] print(most_common) # ๐๏ธ one # ๐๏ธ [('one', 3)] print(counter.most_common(1))
Python indexes are zero-based, so the first item in a list has an index of 0
,
and the last item has an index of -1
or len(my_list) - 1
.
If no argument is provided to the most_common
method, it lists all element
counts.
You can also use this approach to get the most common N elements in a list.
from collections import Counter a_list = ['one', 'one', 'one', 'two', 'two', 'three'] counter = Counter(a_list) n = 2 most_common_n = counter.most_common(n) print(most_common_n) # ๐๏ธ [('one', 3), ('two', 2)]
We passed 2 as an argument to the most_common()
method, so it returned the
most common 2 elements in the list.
Alternatively, you can use the max()
function.
This is a four-step process:
max()
function.key
argument to the functions.list.count()
method to count the occurrences of each element.max()
function will return the most common element.a_list = ['one', 'one', 'one', 'two', 'two', 'three'] most_common = max(set(a_list), key=a_list.count) print(most_common) # ๐๏ธ one
We used the max()
function to get the most common element in a list.
The max function returns the largest item in an iterable or the largest of two or more arguments.
my_list = [15, 45, 30] result = max(my_list) print(result) # ๐๏ธ 45
The max
function also takes an optional key
argument.
key
argument specifies a one-argument ordering function.We used the set() class
to convert the list to a set
object to remove any duplicates.
You can imagine that:
list.count()
method gets called with each element in the set
.max()
function returns the most common list element.a_list = ['one', 'one', 'one', 'two', 'two', 'three'] most_common = max(set(a_list), key=a_list.count) print(most_common) # ๐๏ธ one
The list.count()
method takes a value and returns the number of times the
provided value appears in the list.
a_list = ['one', 'one', 'one', 'two', 'two', 'three'] print(a_list.count('one')) # ๐๏ธ 3 print(a_list.count('two')) # ๐๏ธ 2 print(a_list.count('abc')) # ๐๏ธ 0
Alternatively, you can use the statistics.mode()
method.
The mode()
method returns the single most common value in the provided
iterable.
from statistics import mode a_list = ['one', 'one', 'one', 'two', 'two', 'three'] most_common = mode(a_list) print(most_common) # ๐๏ธ one
The statistics.mode() method takes an iterable and returns the most common value in the iterable.
from statistics import mode a_list = ['one', 'one', 'one', 'two', 'two', 'three'] print(mode([1, 1, 1, 2, 2, 3])) # ๐๏ธ 1 # ๐๏ธ one print(mode(['one', 'one', 'one', 'two', 'two', 'three']))
If there are multiple values with the same frequency, the method returns the first encountered value.
If the provided iterable is empty, the method raises a StatisticsError
exception.
Prior to Python v3.8, the statistics.mode()
method raises a StatisticsError
exception if there is no most common element in the iterable, e.g. if the top
two have the same number of occurrences.
To get the least common element in a list:
collections.Counter()
class to count the occurrences of each
element.most_common()
method to get the most common elements and their
counts.-1
to get the least common element.from collections import Counter a_list = ['c', 'a', 'a', 'b', 'b', 'z', 'z', 'z'] # โ Get the least common element in a list least_common = Counter(a_list).most_common()[-1] print(least_common) # ๐๏ธ ('c', 1) print(least_common[0]) # ๐๏ธ 'c' # ------------------------------------------------- # โ Get the N least common elements in a list least_common_2_elements = Counter(a_list).most_common()[-2:] print(least_common_2_elements) # ๐๏ธ [('b', 2), ('c', 1)]
We used the collections.Counter
class to count the occurrences of each list
item.
The
Counter
class from the collections
module is a subclass of the dict
class.
The class is basically a mapping of key-count pairs.
from collections import Counter a_list = ['c', 'a', 'a', 'b', 'b', 'z', 'z', 'z'] # ๐๏ธ Counter({'z': 3, 'a': 2, 'b': 2, 'c': 1}) print(Counter(a_list))
Counter
objects implement a
most_common()
method that returns a list of the N most common elements and their counts from
the most common to the least.
from collections import Counter a_list = ['c', 'a', 'a', 'b', 'b', 'z', 'z', 'z'] # ๐๏ธ [('z', 3), ('a', 2), ('b', 2), ('c', 1)] print(Counter(a_list).most_common())
We didn't provide an argument for N, so the method returns all elements in the counter.
Elements that have equal counts are ordered by the first encountered value.
The last step is to access the list at index -1
.
from collections import Counter a_list = ['c', 'a', 'a', 'b', 'b', 'z', 'z', 'z'] least_common = Counter(a_list).most_common()[-1] print(least_common) # ๐๏ธ ('c', 1) print(least_common[0]) # ๐๏ธ 'c'
Python indexes are zero-based, so the first item in a list has an index of 0
,
and the last item has an index of -1
or len(my_list) - 1
.
my_list[-1]
returns the last item in the list and my_list[-2]
returns the second to last item.The list element at index -1
stores the least common item and its number of
occurrences in the list.
You can use list slicing if you need to get the N least common elements in the list.
from collections import Counter a_list = ['c', 'a', 'a', 'b', 'b', 'z', 'z', 'z'] least_common_2_elements = Counter(a_list).most_common()[-2:] print(least_common_2_elements) # ๐๏ธ [('b', 2), ('c', 1)]
The syntax for list slicing is my_list[start:stop:step]
.
The start
index is inclusive and the stop
index is exclusive (up to, but not
including).
The slice my_list[-2:]
starts at the second to last list item and goes to the
end of the list.
To find the most frequent value in a dictionary:
dict.values()
method to get a view of the dictionary's values.collections.Counter()
class to create a counter object.most_common()
method to get the most frequent value in the
dictionary.from collections import Counter my_dict = { 'a': 1, 'b': 1, 'c': 1, 'd': 2, 'e': 2, 'f': 3 } counter = Counter(my_dict.values()) most_common_value = counter.most_common(1)[0] print(most_common_value) # ๐๏ธ (1, 3) print(most_common_value[0]) # ๐๏ธ 1 most_common_values = counter.most_common(2) print(most_common_values) # ๐๏ธ [(1, 3), (2, 2)]
The dict.values() method returns a new view of the dictionary's values.
from collections import Counter my_dict = { 'a': 1, 'b': 1, 'c': 1, 'd': 2, 'e': 2, 'f': 3 } # ๐๏ธ dict_values([1, 1, 1, 2, 2, 3]) print(my_dict.values())
We used the collections.Counter
class to count the occurrences of each value.
The
Counter
class from the collections
module is a subclass of the dict
class.
The class is basically a mapping of key-count pairs.
from collections import Counter my_dict = { 'a': 1, 'b': 1, 'c': 1, 'd': 2, 'e': 2, 'f': 3 } counter = Counter(my_dict.values()) print(counter) # ๐๏ธ Counter({1: 3, 2: 2, 3: 1})
Counter
objects implement a
most_common()
method that returns a list of the N most common elements and their counts from
the most common to the least.
from collections import Counter my_dict = { 'a': 1, 'b': 1, 'c': 1, 'd': 2, 'e': 2, 'f': 3 } counter = Counter(my_dict.values()) most_common_value = counter.most_common(1)[0] print(most_common_value) # ๐๏ธ (1, 3) print(most_common_value[0]) # ๐๏ธ 1
Python indexes are zero-based, so the first item in a list has an index of 0
,
and the last item has an index of -1
or len(my_list) - 1
.
If no argument is provided to the most_common
method, it lists all element
counts.
You can also use this approach to get the most common N values in a dictionary.
from collections import Counter my_dict = { 'a': 1, 'b': 1, 'c': 1, 'd': 2, 'e': 2, 'f': 3 } counter = Counter(my_dict.values()) most_common_values = counter.most_common(2) print(most_common_values) # ๐๏ธ [(1, 3), (2, 2)]
We passed 2 as an argument to the most_common()
method, so it returned the
most common 2 dictionary values.
Alternatively, you can use the max
function.
This is a four-step process:
max()
function.key
argument to the function.list.count()
method to count the occurrences of each value.max()
function will return the most common value.my_dict = { 'a': 1, 'b': 1, 'c': 1, 'd': 2, 'e': 2, 'f': 3 } values = list(my_dict.values()) most_common_value = max(set(values), key=values.count) print(most_common_value) # ๐๏ธ 1
We used the dict.values()
method to get a view of the dictionary's values and
converted the view object to a list.
The max() function returns the largest item in an iterable or the largest of two or more arguments.
my_list = [15, 45, 30] result = max(my_list) print(result) # ๐๏ธ 45
The max
function also takes an optional key
argument.
key
argument specifies a one-argument ordering function.We used the set()
class to convert the list to a set
object to remove any
duplicates.
You can imagine that:
list.count()
function gets called with each element in the set
.max()
function returns the most common value.my_dict = { 'a': 1, 'b': 1, 'c': 1, 'd': 2, 'e': 2, 'f': 3 } values = list(my_dict.values()) most_common_value = max(set(values), key=values.count) print(most_common_value) # ๐๏ธ 1
The list.count()
method takes a value and returns the number of times the
provided value appears in the list.
my_dict = { 'a': 1, 'b': 1, 'c': 1, 'd': 2, 'e': 2, 'f': 3 } values = list(my_dict.values()) print(values) # ๐๏ธ [1, 1, 1, 2, 2, 3] print(values.count(1)) # ๐๏ธ 3 print(values.count(2)) # ๐๏ธ 2 print(values.count('abc')) # ๐๏ธ 0
Which approach you pick is a matter of personal preference. I'd use the
Counter
class as I find the most_common
method quite intuitive.
You can learn more about the related topics by checking out the following tutorials: