patrickloeber / blogposts-pythonengineer

Repo with articles for the python-engineer.com website. New authors are welcome!
https://www.python-engineer.com/
21 stars 14 forks source link

How to count the occurrences of a list item? #28

Closed patrickloeber closed 2 years ago

Tamakuwa commented 2 years ago
import collections

item_list = ["foo", "bar", "foo", "foo", "bar", "foobar"]

occurrences = collections.Counter(item_list)

print(occurrences) #print(occurrences["foo"]) to print one element only

output : Counter({'foo': 3, 'bar': 2, 'foobar': 1})

pratik-choudhari commented 2 years ago

assign me