IniziaInizia gratis

Using Counter on lists

Counter is a powerful tool for counting, validating, and learning more about the elements within a dataset that is found in the collections module. You pass an iterable (list, set, tuple) or a dictionary to the Counter. You can also use the Counter object similarly to a dictionary with key/value assignment, for example counter[key] = value.

A common usage for Counter is checking data for consistency prior to using it, so let's do just that.

Questo esercizio fa parte del corso

Data Types in Python

Visualizza il corso

Istruzioni dell'esercizio

  • Import the Counter object from collections.
  • Create a Counter of the penguins list called penguins_sex_counts; use a list comprehension to return the Sex of each penguin to the Counter.
  • Print the penguins_sex_counts.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# Import the Counter object
____

# Create a Counter of the penguins sex using a list comp
penguins_sex_counts = ____

# Print the penguins_sex_counts
____
Modifica ed esegui il codice