ใช้ list class count function นับ occurrences ของ object ใน list ใช้เมื่อต้องการนับ object เดียว หาทั้งหมดของ object ที่ส่งเข้าใน list
ตัวอย่าง
>> ["red", "blue", "red", "red", "blue"].count("red")
3
หากต้องการนับทุก object ใน list ใช้ Counter จาก collections นับ frequency ทุก object คืน dictionary keys=objects values=count
ตัวอย่าง
from collections import Counter
my_list = ["red", "blue", "red", "red", "blue"]
print(Counter(my_list))
ผลลัพธ์
Counter({'blue': 2, 'red': 3})