Whenever I run the below code, i receive an error of dictionary changed size during iteration. I am simply trying to put a list into a dictionary with the key being the element inside the list and the value being the count (number of time it appears).
I want the output to be the max value - each value. If the max value is equal to any value inside the dictionary, remove it.
Here is my code:
from collections import Counter
elements=["a", "b", "abc", "c", "a"]
z=Counter(elements)
q=max(z.values()) #get max number
for x in z:
if z[x] < q:
z[x] = q-z[x]
else:
del z[x] #I am getting an error because of this line of code.
print(z)
Any idea on how to get around this error so the output will look like this:
{'b': 1, 'abc': 1, 'c': 1}