Counting made easy

Data Types in Python

Jason Myers

Instructor

Collections Module

  • Part of Standard Library
  • Advanced data containers
Data Types in Python

Counter

  • Special dictionary used for counting data, measuring frequency
from collections import Counter

nyc_eatery_count_by_types = Counter(nyc_eatery_types)
print(nyc_eatery_count_by_type)
Counter({'Mobile Food Truck': 114, 'Food Cart': 74, 'Snack Bar': 24,
'Specialty Cart': 18, 'Restaurant': 15, 'Fruit & Vegetable Cart': 4})
print(nyc_eatery_count_by_types['Restaurant'])
15
Data Types in Python

Counter to find the most common

  • .most_common() method returns the counter values in descending order
print(nyc_eatery_count_by_types.most_common(3))
[('Mobile Food Truck', 114), ('Food Cart', 74), ('Snack Bar', 24)]
Data Types in Python

Let's practice!

Data Types in Python

Preparing Video For Download...