Creating a dictionary from a list of integers

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

Problem

Given a list of integers, return a dictionary where the keys are the integers and the values are the number of times the integer appears in the list.
Example input: [1, 2, 1, 3, 2, 4, 3, 4, 5, 6, 7, 8, 9, 10]
Example output: {1: 2, 2: 2, 3: 2, 4: 2, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 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 keys are the integers and the values are 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, 3, 2, 4, 3, 4, 5, 6, 7, 8, 9, 10]))

A.I. Evaluation of the Solution

This solution is complete and solves the problem. The approach is straightforward and easy to follow.

Evaluated at: 2023-01-31 04:15:30