Finding most common elements
Another powerful usage of Counter
is finding the most common elements in
a list. This can be done with the .most_common()
method.
Practice using this now to find the most common species in a penguins list.
This exercise is part of the course
Data Types in Python
Exercise instructions
- Import the
Counter
object fromcollections
. - Create a
Counter
of thepenguins
list calledpenguins_species_counts
; use a list comprehension to return theSpecies
of each penguin to the Counter. - Print the three most common species counts.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the Counter object
____
# Create a Counter of the penguins list: penguins_species_counts
penguins_species_counts = ____
# Find the 3 most common species counts
print(____)