Counting the occurrences of integers in a list

Given a list of integers, this function will return a dictionary with the integers as keys and the number of times each integer appears in the list as values.

Problem

Given a list of integers, return a dictionary where the key is the integer and the value is the number of times the integer appears in the list.
Example input: [1, 2, 1, 2, 3, 4, 5]
Example output: {1: 2, 2: 2, 3: 1, 4: 1, 5: 1}

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def count_list(lst):
    
    This function takes a list of integers and returns a dictionary where the key is the integer and the value is the number of times the integer appears in the list.
    
    # create an empty dictionary
    d = {}
    # loop through the list
    for i in lst:
        # if the integer is not in the dictionary, add it as a key and set the value to 1
        if i not in d:
            d[i] = 1
        # if the integer is already in the dictionary, increment the value by 1
        else:
            d[i] += 1
    # return the dictionary
    return d

# test the function
print(count_list([1, 2, 1, 2, 3, 4, 5]))

A.I. Evaluation of the Solution

The candidate's solution correctly solves the problem and demonstrates a level of completeness. The candidate's approach is clear and easy to follow.

Evaluated at: 2022-11-14 02:15:45