I have a list of elements [1, 2, 5, 2, 3, 7, 5, 8]. I want to put it into a dictionary so it would go "key : how many times it appears in the list", so the dictionary looks something like as follows:
{"1:1", "2:2", "5:2", "3:1", "7:1", "8:1"}
The solution should be applicable to any list.
I did the iteration of the list, but am receiving an error when it comes to adding the elements into the dictionary.
given = (1, 2, 5, 2, 3, 7, 5, 8)
midwayDict = dict()
for element in given:
midwayDict = midwayDict.get(element, 0)
print(midwayDict)
All it gives me is " AttributeError: 'int' object has no attribute 'get' ".
Is something wrong with the method I'm using? or should I use a different way?
I saw this being used somewhere a while ago, but I am not being able to find where of how exactly to do this.