I have been trying to create a dictionary consisting of pairs of keys and an array of tuples. Each "instrument" within the "subportfolio" would be one of such tuples, and my idea is to populate a dictionary based on one of the tuple fields containing the key for the dictionary. The code below shows my current implementation:
def assetClassBreakdown(subclasses, subportfolio):
instrumentsDictionary = dict.fromkeys(subclasses, [])
for instrument in subportfolio:
# The subclass is in the ninth index within the instrument
instrumentsDictionary[instrument[8]].append(instrument)
return instrumentsDictionary
The problem is that every time I append a tuple based on the key (instrument[8]), it is appending this tuple to every key inside the dictionary. Basiclly, for every key I end up with exactly the same array of tuples.