0

I have a nested dict of empty lists:

mom_fut = {'M2K': {'comm': [], 'mtm': []},
           'MNQ': {'comm': [], 'mtm': []},
           'NG': {'comm': [], 'mtm': []},
           'QM': {'comm': [], 'mtm': []},
           'VIX': {'comm': [], 'mtm': []},
           'ZS': {'comm': [], 'mtm': []},
           'ZW': {'comm': [], 'mtm': []}}

I am iterating through a list of strings that may contain the main dict keys. If the main key is present, I append corresponding values (from a separate df) to the respective nested lists ('comm' and 'mtm'):

for des in mom_fut_des:
    symb, mat = des.split()
        mom_fut[symb]['comm'].append(mtm_fut_mom.commissions[mtm_fut_mom.description == des].sum())
        mom_fut[symb]['mtm'].append(mtm_fut_mom.total[mtm_fut_mom.description == des].sum())

However, instead of appending the corresponding values to the respective key-value lists and returning the populated dict that I want (and expected):

{'M2K': {'comm': [-25.5777713], 'mtm': [-217.5835163]},
'MNQ': {'comm': [-1.8012515], 'mtm': [-3.7174765]},
 'NG': {'comm': [-12.7160691], 'mtm': [-326.9769691]},
 'QM': {'comm': [-1.5866343], 'mtm': [-49.4922593]},
 'VIX': {'comm': [-1.8242462], 'mtm': [-97.6354962]},
 'ZS': {'comm': [-12.9690108], 'mtm': [-415.3762608]},
 'ZW': {'comm': [-15.1305126], 'mtm': [-235.4963876]}}

It is returning all the values to each key's respective nested list:

{'M2K': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'MNQ': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'NG': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'QM': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'VIX': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'ZS': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'ZW': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]}}

I can get the "correct" output adjusting the code and using dict.update()

for des in mom_fut_des:
    symb, mat = des.split()
    comm = mtm_fut_mom.commissions[mtm_fut_mom.description == des].sum()
    mtm = mtm_fut_mom.total[mtm_fut_mom.description == des].sum()
    mom_fut.update({symb:{'comm':[comm],'mtm':[mtm]}})

But I need to use lists and append since I may have more than one instance of each key, so potentially multiple values in each nested list.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

0

Problem solved.

The issue lay in the way the dict was created. I had used dict.fromkeys() which I was unaware actually "uses the same object for all values... That means, all keys share the same empty list... When you try to .append to the value of one list, the changes are made in-place to the object, so changes to it are visible by all that reference it." See Append value to one list in dictionary appends value to all lists in dictionary

By instead using dict comprehension to create the dict, the issue was resolved and iterating and appending worked as expected.