SyntaxFix
Write A Post
Hire A Developer
Questions
L = ['apple','red','apple','red','red','pear'] d = {} [d.__setitem__(item,1+d.get(item,0)) for item in L] print d
Gives {'pear': 1, 'apple': 2, 'red': 3}
{'pear': 1, 'apple': 2, 'red': 3}