I have a list like this:
my_list = [1, 1, 1, 1, 1, 2, 2, 2, 3]
and I want to make a dictionary like this:
result = {1: 5, 2: 3, 3: 1}
# key is unique list items
# and value is the times they have been repeated in list
I could get this done by this piece of code but doesn't look good:
def parse_list(my_list):
result = {}
my_set = set(my_list)
for i in my_set:
result[i] = len([j for j in my_list if j == i])
return result
I think this should be achievable with less loops. Any idea?