Solution 1

def count_occurrences(a_list):
    result_dict = {}
    for elem in a_list:
        if elem not in result_dict.keys():
            result_dict[elem] = 1
        else:
            result_dict[elem] += 1
    return result_dict

Count Occurrences

Write a function that receives a list as input and returns a dictionary that counts how many times the data in the list is repeated.

count_occurrences(["a", "b", "c", "a", "a," "b"])

# {"a" : 3, "b" : 2, "c": 1}

Test Cases

test count occurrences not unique - Run Test

def test_count_occurrences_not_unique():
    assert count_occurrences(["a", "b", "c", "a", "a", "b"]) == {
        "a": 3,
        "b": 2,
        "c": 1
    }

test count occurrences with numbers - Run Test

def test_count_occurrences_with_numbers():
    assert count_occurrences([12, 42, 42]) == {42: 2, 12: 1}

test count occurrences all unique - Run Test

def test_count_occurrences_all_unique():
    assert count_occurrences(["a", "b", "c"]) == {"a": 1, "b": 1, "c": 1}
def count_occurrences(a_list): pass