I have a list of stocks and positions as tuples. Positive for buy, negative for sell. Example:
p = [(\'AAPL\', 50), (\'AAPL\', -50), (\'RY\', 100), (\'RY\',
How about this? You can read about collections.defaultdict.
>>> from collections import defaultdict >>> testDict = defaultdict(int) >>> p = [('AAPL', 50), ('AAPL', -50), ('RY', 100), ('RY', -43)] >>> for key, val in p: testDict[key] += val >>> testDict.items() [('AAPL', 0), ('RY', 57)]