I am trying to find the add together the values of a list that are matching a value from another list. My logic is the following:
for all the values in list1, count how many times each values of list1 are in list2, add them together and append them to a dictionary.
my current code is not giving me what I am expected and I am failing to understand why.
list1 = [1,2,3,4,5]
list2= [1,1,1,2,2,2,3,3,4,4,5]
count = dict()
for i in list1:
if i in list2:
count[i] = sum(i in list1 for i in list2)
else:
count[i] = 1
print(count)
As a result I get this: {1: 11, 2: 11, 3: 11, 4: 11, 5: 11}
and I am trying to find: {1:3, 2:3, 3:2, 4:2, 5:1}
Thank you for any help!