1
# Trying to Update a multi-dimensional dictionary using a multi-dimensional dictionary
# Iterate through existing dictionary with entries to create new multi-dimensional structured dictionaries
for entry_index, entry_data in entries.items():
        #print(f"Entry[{entry_index}]: {entry_data}")
        client_id = int(entry_data['CLIENT_ID'])
        matter_id = int(entry_data['MATTER_ID'])
        ref_num = entry_data['REF_NUM']
        tk_id = entry_data['TK_ID']
        date = entry_data['DATE'].strftime('%m-%d-%y')

        update_hierarchy = {
            client_id:{
                matter_id:{
                    ref_num:{
                        tk_id:{
                            date:{
                                entry_index:{
                                    'PHASE_TASK': entry_data['PHASE_TASK'],
                                    'ACTIVITY': entry_data['ACTIVITY'],
                                    'RATE': entry_data['RATE'],
                                    'HOURS': float(entry_data['HOURS']),
                                    'AMOUNT': entry_data['AMOUNT'],
                                    'DESCRIPTION': entry_data['DESCRIPTION'],
                                    'HOLD': entry_data['HOLD']
                                }
                            }
                        }
                    }
                }
            }
        }
        entries_hierarchy.update(update_hierarchy)
        # Expected behavior: Each nested key will be updated given nested dict structure...ie.
        # Expected Output:
        '''
        800:
            50:
                24:
                    12:
                        12.21.21:
                            0:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
                            1:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
            51:
                24:
                    12:
                        12.21.21:
                            2:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
                            3:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
        '''
        # Actual Result
        '''
        800:
            50:
                24:
                    12:
                        12.21.21:
                            0:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
                            1:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
        '''

        update_clients = {
            client_id: {
                entry_index: {
                    'MATTER_ID': matter_id,
                    'REF_NUM': ref_num,
                    'TK_ID': tk_id,
                    'DATE': date,
                    'PHASE_TASK': entry_data['PHASE_TASK'],
                    'ACTIVITY': entry_data['ACTIVITY'],
                    'RATE': entry_data['RATE'],
                    'HOURS': float(entry_data['HOURS']),
                    'AMOUNT': entry_data['AMOUNT'],
                    'DESCRIPTION': entry_data['DESCRIPTION'],
                    'HOLD': entry_data['HOLD']
                }
            }
        }
        entries_clients.update(update_clients)

The entries_clients.update(update_clients) ends up with only 1 entry per client id and expected behavior would be for update() to insert a new entry_id key under the client_id key if it does not exist

It seems Update() will only update the first key in a dict and ignores the nested keys...I am coming from PHP and finding working with arrays in Python not so intuitive.

Other questions I have found only talk about a single nested dctionary and not a multi-level one as such.

martineau
  • 119,623
  • 25
  • 170
  • 301
blindminds
  • 13
  • 2
  • 2
    When asking questions it is preferable to post only the *minimum* code necessary to illustrate the problem (i.e. [mre]). – martineau Dec 24 '21 at 15:54
  • 1
    This thread might help you : https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth – Sami Tahri Dec 24 '21 at 16:56
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 02 '22 at 21:10

1 Answers1

1

It looks like you've updated the wrong dict.

On the last line try changing it from:

entries_clients.update(update_clients)

to

entries_clients[client_id].update(update_clients)
Daring_T
  • 136
  • 1
  • 5
  • I am not trying to update the wrong dict, I am trying to update the entire dict. I am trying to update these nested dict's so that for instance, the entries_clients example, if the client_id already exists, and the entry_iindex does not, it adds the entry_index key:value pair to the corresponding client_id key. But in both instances, it just updates the first key and not the entire dict. – blindminds Dec 25 '21 at 01:08