Write a Python function to count the occurrence of each element in a list.
def count_elements(lst):
count_dict = {}
for element in lst:
if element in count_dict:
count_dict[element] += 1
else:
count_dict[element] = 1
return count_dict
# Test the function
my_list = [1, 2, 3, 2, 4, 1, 3, 4, 4, 5]
element_count = count_elements(my_list)
print(element_count)
Comments
Post a Comment