Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Data Types in Python

Cursus bekijken

Oefeninstructies

  • Import the Counter object from collections.
  • Create a Counter of the penguins list called penguins_species_counts; use a list comprehension to return the Species of each penguin to the Counter.
  • Print the three most common species counts.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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(____)
Code bewerken en uitvoeren