Issues updating values in dictionary

Viewed 26

I want to do the following query:

I am creating a dictionary from data that comes from a CSV. I have a key that has different values, so I added this line of code looking if that ID previously exists in the dictionary, add those values ​​to that key. If it doesn't exist, create the values.

if item_id in item_data:
   item_data [item_id] + = [subtitle_id, subtitle_name, original_amount, amount_to_march, currency]
else:
   item_data [item_id] = [subtitle_id, subtitle_name, original_amount, amount_to_march, currency]

The problem is that the output generated is something like this:

{'01': ['08',
  'OTHER CURRENT INCOME',
  311948.0,
  311948.0,
  'CLP',
  '09',
  'TAX CONTRIBUTION',
  18414647.0,
  18536395.0,
  'CLP',
  '10',
  'SALE OF NON-FINANCIAL ASSETS',
  16929.0,
  16929.0,
  'CLP',
  '15',
  'INITIAL CASH BALANCE',
  1000.0,
  842590.0,
  'CLP',
  '21',
  'PERSONNEL EXPENSES',
  8189139.0,
  8189139.0,
  'CLP']

And what I need is something like this:

{'01': {'subtitle_id':'08',
  'subtitle_name':'OTHER CURRENT INCOME',
  'original_amount':311948.0,
  'amount_to_march':311948.0,
  'currency': 'CLP'},
  {'subtitle_id':'09',
  'subtitle_name':'TAX CONTRIBUTION',
  'original_amount':18414647.0,
  'amount_to_march':18536395.0,
  'currency':'CLP'},
  {'subtitle_id':'10',
  'subtitle_name':'SALE OF NON-FINANCIAL ASSETS',
  'original_amount':16929.0,
  'amount_to_march':16929.0,
  'currency':'CLP'},
  {'subtitle_id':'15',
  'subtitle_name':'INITIAL CASH BALANCE',
  'original_amount':1000.0,
  'amount_to_march':842590.0,
  'currency':'CLP'},
  {'subtitle_id':'21',
  'subtitle_name':'PERSONNEL EXPENSES',
  'original_amount':8189139.0,
  'amount_to_march':8189139.0,
  'currency':'CLP'}

How could I create such a nested dictionary, so to speak?

Thanks a lot!

1 Answers

Your values in the dictionary will still need to be lists since what you've put in your second example isn't going to be valid. In any case, the change you mostly need is to just append dictionaries to each list instead of extending the way you are. It's also a good case for using defaultdict so you don't need to both with the extra lines checking for a key's existence.

Here's an example that should be relatively easy to adapt:

In [93]: from collections import defaultdict

In [94]: d = defaultdict(list)

In [95]: for i in range(10):
    ...:     d[i % 3].append({'subtitle_id': i, 'subtitle_name': i**2, 'currency': 'CLP'})
    ...:

In [96]: d
Out[96]:
defaultdict(list,
            {0: [{'subtitle_id': 0, 'subtitle_name': 0, 'currency': 'CLP'},
              {'subtitle_id': 3, 'subtitle_name': 9, 'currency': 'CLP'},
              {'subtitle_id': 6, 'subtitle_name': 36, 'currency': 'CLP'},
              {'subtitle_id': 9, 'subtitle_name': 81, 'currency': 'CLP'}],
             1: [{'subtitle_id': 1, 'subtitle_name': 1, 'currency': 'CLP'},
              {'subtitle_id': 4, 'subtitle_name': 16, 'currency': 'CLP'},
              {'subtitle_id': 7, 'subtitle_name': 49, 'currency': 'CLP'}],
             2: [{'subtitle_id': 2, 'subtitle_name': 4, 'currency': 'CLP'},
              {'subtitle_id': 5, 'subtitle_name': 25, 'currency': 'CLP'},
              {'subtitle_id': 8, 'subtitle_name': 64, 'currency': 'CLP'}]})
Related