I created a function that counts the number of times a string has appeared in a list and returns a dictionary. I got the program to work, but interestingly enough, when I changed the value of a key value, d[i], the order of the names changed . Why is python doing this?
Before:
def countlist(l):
d = {}
for i in l:
if i not in d:
d[i] = 0
else:
d[i] += 1
return d
input:
countlist(['helen','andrew','chris','chris','helen'])
result:
{'helen': 1, 'andrew': 0, 'chris': 1}
After:
def countlist(l):
d = {}
for i in l:
if i not in d:
d[i] = 1
else:
d[i] += 1
return d
input: countlist(['helen','andrew','chris','chris','helen'])
result: {'andrew': 1, 'chris': 2, 'helen': 2}