Python Beginner Concepts Tutorial

Count Occurences of Each Unique Element in a List

Import Libraries

from collections import Counter
from collections import defaultdict

Count Occurences of Each Unique Element - 3 Different Ways

Let's say your friend gets a list of grades for the semester. We want a count for each letter grade.

high_school_semester_grades = ["B+", "A", "B+", "A", "A+", "A-", "A"]

There are several Pythonic ways to easily do this.

Create a Dictionary

The dictionary object is built into Python's standard library.

dict_count_occurences_of_letters = {}

for letter in high_school_semester_grades:
    if letter in dict_count_occurences_of_letters:
        dict_count_occurences_of_letters[letter] += 1
        dict_count_occurences_of_letters[letter] = 1
{'A': 3, 'A+': 1, 'A-': 1, 'B+': 2}

Create a defaultdict

We import the defaultdict subclass from the Collections module in Python.

defaultdict_count_occurences_of_letters = defaultdict(int)
for letter in high_school_semester_grades:
    defaultdict_count_occurences_of_letters[letter] += 1
defaultdict(int, {'A': 3, 'A+': 1, 'A-': 1, 'B+': 2})

Create a Counter

We import the Counter subclass from the Collections module in Python.

counter_count_of_letters = Counter(high_school_semester_grades)
Counter({'A': 3, 'A+': 1, 'A-': 1, 'B+': 2})

Additional: Sort Data Structures by Count of Letters

Dictionary sort values

sorted(dict_count_occurences_of_letters.items(), key=lambda x: x[1], reverse=True)
[('A', 3), ('B+', 2), ('A+', 1), ('A-', 1)]

defaultdict sort values

Same method as the dictionary way above.

sorted(defaultdict_count_occurences_of_letters.items(), key=lambda x: x[1], reverse=True)
[('A', 3), ('B+', 2), ('A+', 1), ('A-', 1)]

Counter sort values

Counter has a built-in method called most_common to return a list of tuples sorted from most common to least common.

[('A', 3), ('B+', 2), ('A+', 1), ('A-', 1)]

Evaluation of Three Data Structures

For the above scenario, I prefer Counter because it involves the most concise and easy to read code. There's a lot of operations on the background, but that's abstracted away from us.

Just be careful using unique structures such as Counter because other programmers may be unfamiliar with them. I'd recommend to link to the relevant Python official documentation.