I have a list of objects and I would like to see which ones are the most common. It is easy to find the or a most common object using python's Counter
, but I haven't found a simple way to find all of the most common objects. The code below is working but I would guess there's a better way to do this.
from collections import Counter
input_lst = [1, 2, 3, 3, 4, 4, 0, 1]
counts = Counter(input_lst)
ranked_counts = counts.most_common().copy()
first_common_obj = ranked_counts.pop(0)
equally_common_objs = [first_common_obj[0]]
while ranked_counts:
next_obj = ranked_counts.pop(0)
if first_common_obj[1] == next_obj[1]:
equally_common_objs.append(next_obj[0])
else:
break
print(equally_common_objs)